question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Click button if present, else click a different button

See original GitHub issue

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: 1.4.0
  • Platform / OS version: MacOS 10.12.6
  • URLs (if applicable):
  • Node.js version: 8.9.1

What steps will reproduce the problem?

Please include code that reproduces the issue.

I am trying to set up an if/else statement in puppeteer to click on a button if it is present, else click on other button first, then click on the button I am trying to get to. I am doing something like this:

if ((await page.$('#buttonToClick')) !== null) {
   await page.click('#buttonToClick');
 } else {
   await page.waitForSelector('#otherButton');
   await page.click('#otherButton');

   await page.waitForSelector('#buttonToClick');
   await page.click('#buttonToClick');
 }

For some reason I keep falling into the else block even when I go to my Chrome console and do a document.querySelector('#buttonToClick') !==null on the desired page and it is showing as true

I also tried doing:

await page.waitForNavigation({ waitUntil: 'networkidle0' });
if (await page.$('#buttonToClick') !== null) {
    await page.click('#buttonToClick');
  } else {
    await page.waitForSelector('#otherButton');
    await page.click('#otherButton');

    await page.waitForSelector('#buttonToClick');
    await page.click('#buttonToClick');
  }

But that didn’t work either. I also swapped out the above line: await page.waitForNavigation({ waitUntil: 'networkidle0' }); for await page.waitForNavigation({ waitUntil: 'domcontentloaded' }); and also tried await page.waitForNavigation({ waitUntil: 'networkidle2' }); and none of those seemed to work.

The ONLY way I can get the expected result I am looking for is if I write the following code:

await page.waitFor(1000);

  if ((await page.$('#buttonToClick')) !== null) {
    await page.click('#buttonToClick');
  } else {
    await page.waitForSelector('#otherButton');
    await page.click('#otherButton');

    await page.waitForSelector('#buttonToClick');
    await page.click('#buttonToClick');
  }

But I don’t necessarily like the idea of having to do a page.waitFor() for a fixed number of seconds…any ideas what could be going on? Any help is appreciated, thanks so much in advance!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
flyingskycommented, Sep 28, 2018

A better idea is Promise.race to make it faster, like

await Promise.race([
  page.waitForSelector('#buttonToClick');
  page.waitForSelector('#otherButton')
);

// Now you get one item, you can check its id to know which button is displayed.
1reaction
yanivefraimcommented, May 27, 2018

The main difference is that instead of just having a timeout, you have a ‘waitForSelector’, with a timeout. In case this timeout passes, promise will be rejected (with a timeout error). You can catch this error and have your fallback - click another button. Does it makes sense for your case?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Click button if it is present, else click a different button in ...
I am trying to set up an if/else statement in puppeteer to click on a button if it is present, else click on...
Read more >
Click one of 2 button (know the exist one and click it ) - Help
I got 2 buttons only 1 could appear so how to tell the program to click the one that'll appear.
Read more >
Handle Radio Button in Selenium WebDriver - Tools QA
If the radio button contains a unique value in the "name " attribute, then we can use the Name locator of Selenium to...
Read more >
click - WebdriverIO
This issues a WebDriver click command for the selected element , which generally scrolls to and then clicks the selected element when no...
Read more >
Test Automation With Selenium Click Button Method(Examples)
We facilitate the click interaction using a method called Selenium.click(). Selenium click button method, although, is one of the most basic ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found