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.

Validate multiple selectors in DOM

See original GitHub issue

Related to question

Is it possible to validate with Puppeteer v1.11.0 (or other approach) what is the first selector in appears of a group of selectors and know which one it was to perform an action?

selector1 = `#user`;
selector2 = `#search`;
selector3 = `#error`;

waitForSelector(selector1 OR selector2 OR selector3)

if(selector1){
 //do something
}else if(selector2){
 //do other something
}else if(selector3){
 //do other other something 
}else{
 //finally do...
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
vsemozhetbytcommented, Jan 16, 2019

Something like this?

'use strict';

const puppeteer = require('puppeteer');

(async function main() {
  try {
    const browser = await puppeteer.launch();
    const [page] = await browser.pages();

    await page.goto('https://example.org/');

    const selectors = ['h1', 'p', 'a'];

    const jsHandle = await page.waitForFunction((selectors) => {
      for (const selector of selectors) {
        if (document.querySelector(selector) !== null) {
          return selector;
        }
      }
      return false;
    }, {}, selectors);

    const selector = await jsHandle.jsonValue();

    if (selector === selectors[0]) {
      console.log(`The first selector is '${selectors[0]}'.`);
    } else if (selector === selectors[1]) {
      console.log(`The first selector is '${selectors[1]}'.`);
    } else if (selector === selectors[2]) {
      console.log(`The first selector is '${selectors[2]}'.`);
    } else {
      console.log('Something strange happens.');
    }

    await browser.close();
  } catch (err) {
    console.error(err);
  }
})();
The first selector is 'h1'.
2reactions
dcr007commented, May 20, 2020

how about using Promise.race() something like this : await Promise.race([await this.page.$(selector1), await this.page.$(selector2)]);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using multiple selectors with querySelector(), querySelectorAll ...
// Get's any element with the .sandwich class, and all labels inside the #contact element var elems = document.querySelectorAll('.sandwich, # ...
Read more >
Multiple Selector (“selector1, selector2, selectorN”) - jQuery API
This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object...
Read more >
Document.querySelectorAll() - Web APIs | MDN
A string containing one or more selectors to match against. This string must be a valid CSS selector string; if it's not, a...
Read more >
How to determine if I'm selecting multiple or a single element ...
I'm writing a function $ (similar to one of jQuery's $ ), but with non-jQuery methods and helpers, with plain DOM properties. I...
Read more >
jQuery - Multiple Elements Selector - Tutorialspoint
You can specify any number of selectors to combine into a single result. Here order of the DOM elements in the jQuery object...
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