Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer - wait for element to have certain value

So question is - how to wait until an element have a certain value? For example, some button on certain page changes value of input field from "No Value" to "Value X". Problem is - I don't know how much time will it take so page.waitFor() is not an option. I thought I could use page.waitForSelector(input:contains('No Value')); but that does not work as far as I understand. Possibly page.waitForFunction() should work, but I am not sure what function to write there (page.evaluate that returns value of input maybe?).

like image 644
kmeshavkin Avatar asked May 16 '18 14:05

kmeshavkin


People also ask

How do you wait for an element to load in a puppeteer?

You can use Puppeteer's page. waitForNavigation() method here to explicitly wait for this event to happen and then continue your script. The accepted notation in Puppeteer's case is by using the Promise. all() method to wait for the click to happen and the navigation to happen before continuing.

What does waitForSelector return?

waitForSelector() method. Wait for the selector to appear in page. If at the moment of calling the method the selector already exists, the method will return immediately. If the selector doesn't appear after the timeout milliseconds of waiting, the function will throw.

How do you get the puppeteer element?

Java Prime Pack We can get element text in Puppeteer. This is done with the help of the textContent property. This property of the element is passed as a parameter to the getProperty method.

How do you click a puppeteer button?

click() method. This method fetches an element with selector , scrolls it into view if needed, and then uses Page. mouse to click in the center of the element. If there's no element matching selector , the method throws an error.


1 Answers

According to docs page.waitForFunction will wait until the function passed to it (as a closure or a string) returns a truthy value.

const puppeteer = require('puppeteer');

puppeteer.launch({headless : false}).then(async browser => {
  const page = await browser.newPage();
  await page.goto(url);
  await page.waitForFunction('document.getElementById("wait").value != "No Value"');
  console.log('Value changed!');
  await browser.close();
});
like image 173
Vaviloff Avatar answered Sep 21 '22 23:09

Vaviloff