javascript - Complicated CSS selector in Puppeteer - Stack Overflow

Is there a shorter method to wait for plicated selector?await page.evaluate(() => {return new Promis

Is there a shorter method to wait for plicated selector?

await page.evaluate(() => {
  return new Promise(resolve => {
     var aid = setInterval(function(){
        let block = $('div[class="asset"]:contains("Assets Folder")');

        if(block.length){
           clearInterval(aid);
           block.click();
           resolve();
        }

     }, 100);
  })
});

page.waitFor() throws an error:

Error: Evaluation failed: DOMException: Failed to execute 'querySelector' on 'Document': 'div[class="asset"]:contains("Assets Folder")' is not a valid selector.

Is there a shorter method to wait for plicated selector?

await page.evaluate(() => {
  return new Promise(resolve => {
     var aid = setInterval(function(){
        let block = $('div[class="asset"]:contains("Assets Folder")');

        if(block.length){
           clearInterval(aid);
           block.click();
           resolve();
        }

     }, 100);
  })
});

page.waitFor() throws an error:

Error: Evaluation failed: DOMException: Failed to execute 'querySelector' on 'Document': 'div[class="asset"]:contains("Assets Folder")' is not a valid selector.

Share Improve this question edited Oct 30, 2018 at 18:10 Grant Miller 29.1k16 gold badges156 silver badges170 bronze badges asked Oct 30, 2018 at 11:30 John LensonJohn Lenson 1672 silver badges9 bronze badges 3
  • If i remember correctly, the :contains() selector only works with jQuery. See this question – Martijn Vissers Commented Oct 30, 2018 at 12:01
  • Ok, so maybe there is shorter workaround, without evaluate + promise + setinterval chain? – John Lenson Commented Oct 30, 2018 at 13:28
  • my approach is to use javascript to insert a value to role or name for better selection. Puppeteer currently only supports for selection of name and role. – Alston Commented Jun 13, 2022 at 14:01
Add a ment  | 

1 Answer 1

Reset to default 6

page.waitForFunction()

You can use page.waitForFunction() to wait for a given function to return a truthy value.

Keep in mind that the :contains() selector is part of the jQuery API and not a standard CSS selector.

Therefore, instead, you can wait until some element (at least one) in an array of nodes matching the selector has textContent which includes the specified text and is present in the DOM:

await page.waitForFunction(() =>
  [...document.querySelectorAll('div[class="asset"]')].some(e => e.textContent.includes('Assets Folder'))
);

page.waitForXPath()

Alternatively, XPath includes a contains() function, so you can use page.waitForXPath() with a corresponding XPath expression to wait for your element to be added to the DOM:

await page.waitForXPath('//*[contains(text(), "Assets Folder")]/ancestor-or-self::div[@class="asset"]');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744758062a4592001.html

相关推荐

  • javascript - Complicated CSS selector in Puppeteer - Stack Overflow

    Is there a shorter method to wait for plicated selector?await page.evaluate(() => {return new Promis

    5小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信