I open a website, then wait for all redirects to be done. Then I capture a captcha image, and send it via nodejs to a user. Then I recive the typed captcha:
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto('http://localhost/p1.php' );
await page.waitForNavigation();
const captcha_image = await page.$eval('#security', e => e.getAttribute('src'));
io.emit('send_captcha_to_client' , {text : captcha_image });
var captcha = await captchaPromise;
After I receive the typed value of the capthca, I put it in the field and click the login button:
await page.$eval('#W_CAPTCHA', (el , _captcha) => el.value = _captcha.W_CAPTCHA , captcha );
await page.click('#login-btn');
Now after clicking the button, an ajax request will be sent to the server. Lets say http://example.com/login.php
- if the captcha is right, I will get redirected to my dashboard, but if the ajax call returns lets say {status:er}
And there is <span id="alert"></span>
in the page it'll add .Error
class to it and put the error text in there. How can I intercept the ajax call and check if the login was successful or not?
I can check the result of the ajax call or wait and check the document for div.Error
. But I am not sure how can I do any of them. Here is my failed attempt:
await page.waitForNavigation();
page.waitForSelector('.Error');
const error = await page.$eval('.Error', e => e.value );
console.log(error);
browser.close();
The jQuery Ajax async is handling Asynchronous HTTP requests in the element. It is a procedure to send a request to the server without interruption. It is an Asynchronous method to send HTTP requests without waiting response. It is a function to working on a server without associating more than on request.
Since async/await is just Promise's under the hood, I wonder if I can use async/await with jQuery's $. ajax(). Turns out, you can!
The ajaxStop() method specifies a function to run when ALL AJAX requests have completed. When an AJAX request completes, jQuery checks if there are any more AJAX requests. The function specified with the ajaxStop() method will run if no other requests are pending.
The accepted answer isn't waiting for a request but waiting for a selector. That kind of hacky-way wasn't doing it for me.
Waiting a promise resolution coupled with the request response status is the easiest way to achieve it.
await Promise.all( [
page.click( selector ), // ... action
page.waitForResponse( response => response.status() === 200 ), // ... wait for network
] );
Response | Description |
---|---|
200 OK |
The HTTP 200 OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default. |
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With