Puppeteer Different Behavior with Browser
I am getting differing results between querySelector commands in the console and the equivalent puppeteer code on nodejs. I'm using headless chromium and puppeteer-extra-plugin-stealth, so it probably isn't captcha or anything. Instead it seems to be some type of ad redirect that is messing everything up.
Using querySelector on the console, I can click on the button I want.
document.querySelectorAll("button[class*=OfferCta__]")[1].click()
This opens a modal on the same page. Sometimes it also opens a new tab with another website.
Unfortunately, when I run the equivalent on puppeteer via nodejs:
const buttons = await page.$$("button[class*=OfferCta__]");
await page.waitFor(2000);
await buttons[1].click();
await page.waitFor(6000);
await page.screenshot({ path: "screenshot.png" });
All that shows up is an ad followed by lots of white space and nothing else: 
The page I'm trying to scrape is https://www.retailmenot.com/view/myntra.com
Edit: Something interesting I discovered in the logs. Every two minutes, puppeteer seems to restart, as seen in the logs. It fails as usual the first few times, but on the fifth restart, ten minutes after I ran the code, it runs correctly, which I denoted with "done retrieving". I have a feeling this means that the success condition, whatever it is, is random?
4:44:33 PM web.1 | Listening on 5000
4:44:34 PM web.1 | https://www.retailmenot.com/view/myntra.com
4:44:37 PM web.1 | done waiting
4:44:43 PM web.1 | screenshot happening
4:44:43 PM web.1 | screenshot happened
4:44:43 PM web.1 | 50
4:44:52 PM web.1 | screenshot happening
4:44:52 PM web.1 | screenshot happened
4:44:52 PM web.1 | Error: Error: failed to find element matching selector "div[class^=OfferItemFull__OfferItemWrapper]"
4:44:52 PM web.1 | at ElementHandle.$eval (/Users/mountaindrake/flock-proj/productPin/node_modules/puppeteer-core/lib/cjs/puppeteer/common/JSHandle.js:647:19)
4:44:52 PM web.1 | at process._tickCallback (internal/process/next_tick.js:68:7)
4:46:34 PM web.1 | https://www.retailmenot.com/view/myntra.com
4:46:37 PM web.1 | done waiting
4:46:43 PM web.1 | screenshot happening
4:46:43 PM web.1 | screenshot happened
4:46:43 PM web.1 | 50
4:46:52 PM web.1 | screenshot happening
4:46:52 PM web.1 | screenshot happened
4:46:52 PM web.1 | Error: Error: failed to find element matching selector "div[class^=OfferItemFull__OfferItemWrapper]"
4:46:52 PM web.1 | at ElementHandle.$eval (/Users/mountaindrake/flock-proj/productPin/node_modules/puppeteer-core/lib/cjs/puppeteer/common/JSHandle.js:647:19)
4:46:52 PM web.1 | at process._tickCallback (internal/process/next_tick.js:68:7)
4:48:34 PM web.1 | https://www.retailmenot.com/view/myntra.com
4:48:36 PM web.1 | done waiting
4:48:43 PM web.1 | screenshot happening
4:48:43 PM web.1 | screenshot happened
4:48:43 PM web.1 | 50
4:48:51 PM web.1 | screenshot happening
4:48:52 PM web.1 | screenshot happened
4:48:52 PM web.1 | Error: Error: failed to find element matching selector "div[class^=OfferItemFull__OfferItemWrapper]"
4:48:52 PM web.1 | at ElementHandle.$eval (/Users/mountaindrake/flock-proj/productPin/node_modules/puppeteer-core/lib/cjs/puppeteer/common/JSHandle.js:647:19)
4:48:52 PM web.1 | at process._tickCallback (internal/process/next_tick.js:68:7)
4:50:34 PM web.1 | https://www.retailmenot.com/view/myntra.com
4:50:36 PM web.1 | done waiting
4:50:43 PM web.1 | screenshot happening
4:50:43 PM web.1 | screenshot happened
4:50:43 PM web.1 | 50
4:50:51 PM web.1 | screenshot happening
4:50:52 PM web.1 | screenshot happened
4:50:52 PM web.1 | Error: Error: failed to find element matching selector "div[class^=OfferItemFull__OfferItemWrapper]"
4:50:52 PM web.1 | at ElementHandle.$eval (/Users/mountaindrake/flock-proj/productPin/node_modules/puppeteer-core/lib/cjs/puppeteer/common/JSHandle.js:647:19)
4:50:52 PM web.1 | at process._tickCallback (internal/process/next_tick.js:68:7)
4:52:34 PM web.1 | https://www.retailmenot.com/view/myntra.com
4:52:37 PM web.1 | done waiting
4:52:43 PM web.1 | screenshot happening
4:52:43 PM web.1 | screenshot happened
4:52:43 PM web.1 | 50
4:52:52 PM web.1 | screenshot happening
4:52:52 PM web.1 | screenshot happened
4:52:52 PM web.1 | Error: Error: failed to find element matching selector "div[class^=OfferItemFull__OfferItemWrapper]"
4:52:52 PM web.1 | at ElementHandle.$eval (/Users/mountaindrake/flock-proj/productPin/node_modules/puppeteer-core/lib/cjs/puppeteer/common/JSHandle.js:647:19)
4:52:52 PM web.1 | at process._tickCallback (internal/process/next_tick.js:68:7)
4:54:34 PM web.1 | https://www.retailmenot.com/view/myntra.com
4:54:36 PM web.1 | done waiting
4:54:42 PM web.1 | screenshot happening
4:54:43 PM web.1 | screenshot happened
4:54:43 PM web.1 | 50
4:54:51 PM web.1 | screenshot happening
4:54:52 PM web.1 | screenshot happened
4:54:52 PM web.1 | Error: Error: failed to find element matching selector "div[class^=OfferItemFull__OfferItemWrapper]"
4:54:52 PM web.1 | at ElementHandle.$eval (/Users/mountaindrake/flock-proj/productPin/node_modules/puppeteer-core/lib/cjs/puppeteer/common/JSHandle.js:647:19)
4:54:52 PM web.1 | at process._tickCallback (internal/process/next_tick.js:68:7)
4:56:34 PM web.1 | https://www.retailmenot.com/view/myntra.com
4:56:37 PM web.1 | done waiting
4:56:43 PM web.1 | screenshot happening
4:56:43 PM web.1 | screenshot happened
4:56:43 PM web.1 | 50
4:56:52 PM web.1 | screenshot happening
4:56:52 PM web.1 | screenshot happened
4:56:52 PM web.1 | done retreiving
from Recent Questions - Stack Overflow https://ift.tt/3mpJpnN
https://ift.tt/3jvLuNa
Comments
Post a Comment