Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
48
rated 0 times [  53] [ 5]  / answers: 1 / hits: 23958  / 6 Years ago, wed, june 27, 2018, 12:00:00

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();

More From » ajax

 Answers
7

You can wait on both simultaneously and handle whichever occurs first:



await Promise.race([
page.waitForNavigation({ waitUntil: networkidle0 }),
page.waitForSelector(.Error)
]);

if (await page.$(.Error)) {
// there was an error
} else {
// the page changed
}

[#54107] Friday, June 22, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
domeniccolti

Total Points: 276
Total Questions: 98
Total Answers: 93

Location: India
Member since Fri, May 13, 2022
2 Years ago
domeniccolti questions
Mon, Oct 18, 21, 00:00, 3 Years ago
Thu, Oct 14, 21, 00:00, 3 Years ago
Thu, Jul 15, 21, 00:00, 3 Years ago
Sat, Oct 24, 20, 00:00, 4 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;