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.

[Feature] Auto-wait in page.$eval?

See original GitHub issue

While debugging @mynar7’s issue on slack, I noticed that the page.waitForNavigation was not a reliable way to ensure that the #catalog-title is visible. Replacing it with a page.waitForSelector fixed it.

However, since we are moving towards auto-waits in API calls that refer to element selectors, I was wondering if the page.$eval method could auto-wait for the element, thereby eliminating the need to waitForSelector.

const { firefox, chromium, webkit } = require('playwright');
const assert = require('assert');

(async() => {
    const browser = await firefox.launch();
    const page = await browser.newPage();
    await page.goto('https://realtruck.com/?disableIntegration=all');

    await Promise.all([
        page.click('text=Tonneau Covers'),
        page.waitForSelector('#catalog-title') // was earlier using page.waitForNavigation()
    ]);

    const header = await page.$eval('#catalog-title', el => el.textContent);
    assert(header, "Tonneau Covers");
    await browser.close();
})();

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
aslushnikovcommented, Mar 3, 2020

since we are moving towards auto-waits in API calls that refer to element selectors

We keep thinking in this direction, but there are a few concerns particularly regarding page.$eval:

  • technical: waitForSelector has some interesting options and might get more in future; and there’s no good shape for page.$eval to accept these options.
  • aesthetics: so far we think that it’s nice to have all waiting methods prefixed with waitFor.. - it self-explains the API for non-experienced users.

So we’ll keep thinking about this! For now, I think I can re-write your snippet in a slightly nicer way:

await page.goto('https://realtruck.com/?disableIntegration=all');
await page.click('text=Tonneau Covers');
const catalog = await page.waitForSelector('#catalog-title');
const header = await catalog.evaluate(el => el.textContent);
assert(header, "Tonneau Covers");
2reactions
aslushnikovcommented, Mar 4, 2020

Should that one not wait for the selector to appear?

@thernstig yes, it does not wait for selector to appear. The semantic is the same as the $ in devtools console, or in jquery - they just fetch element from the page.

If you want to wait for a selector, you should use page.waitfor("foo")

Read more comments on GitHub >

github_iconTop Results From Across the Web

Page.$eval() method - Puppeteer
Page.$eval() method. This method runs document.querySelector within the page and passes the result as the first argument to the pageFunction .
Read more >
How to wait for JavaScript to finish in playwright - Stack Overflow
I think you can use setTimeout with page.evaluate inside the page context to wait a bit for other JavaScript to run: await page.evaluate(() ......
Read more >
Timeouts | Checkly
Playwright click and fill methods will auto-wait for the element to be visible. ... 5000 }) const links = await page.evaluate(() => Array.from(document....
Read more >
playwright._impl._api_types.error: execution context was ...
I am just using got function but it gives me the error. ... page.evaluate: Execution context was destroyed, most likely because of a...
Read more >
Core concepts | Playwright 中文文档
Actions like click and fill auto-wait for the element to be visible and ... The page.evaluate API can run a JavaScript function in...
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