I'm trying to do web scraping using puppeteer. The element I need to handle loads lately. When I click on the search button the result loads in AJAX and I need to pick the element I am trying to pick is in the search results but not in the initial load of the page. The page screenshot it is producing contains search results too and if it output the HTML source I can see the element there too. but not sure why I cannot pick it.
Cut and paste whatever code you need to execute in the callback function passed to success . Some good answer is already provided.
GET is basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data. POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.
XMLHttpRequest (XHR) objects are used to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing. XMLHttpRequest is used heavily in AJAX programming.
You can use await page.waitForSelector(cssSelector);
to ask Puppeteer to wait for any element to be displayed in the UI before continuing on to further steps in your script. By default, the timeout for the wait is 30 seconds but you can set it to any timeout you wish.
So in your case I would:
await page.waitForSelector(cssSelector);
to ask Puppeteer to wait until some element you are sure will be displayed in the UI after executing the search is visible.What you might find happens, if you don't use that waitForSelector()
call is that the element is displayed but Puppeteer will timeout, for example, if you wish to execute a click
command on an element. This is because the timeouts for click
events (and other Puppeteer events which interact with elements) is very short and sometimes the script (especially in headless mode) can move to the next instruction too quickly to allow for the UI to update fast enough to keep up.
So by adding the additional waitForSelector
calls, you're also making your scripts much more robust. Especially when data is being generated dynamically as they are in your case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With