When use puppeteer
to scrape a bunch of sites via a for-loop
, whenever a new page is created, the browser would jump to the foreground, which hinders me from doing other things on my computer.
Even I set the following args, it still doesn't work, so how could I keep the browser running quietly without jumping to foreground and interupting me?
I need to run in headful
mode, not headless
mode.
headless: false,
args: [
'--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-renderer-backgrounding',
]
Let’s start our Puppeteer tutorial with a basic example. We’ll write a script that will cause our headless browser to take a screenshot of a website of our choice. Create a new file in your project directory named screenshot.js and open it in your favorite code editor.
Since this aspect is more architecture-related, I won’t cover this in depth in this Puppeteer tutorial. That said, the most basic way to slow down a Puppeteer script is to add a sleep command to it: This statement will force your script to sleep for five seconds (5000 ms). You can put this anywhere before browser.close ().
Puppeteer lets you automate the testing of your web applications. With it, you can run tests in the browser and then see the results in real-time on your terminal. Puppeteer uses the WebDriver protocol to connect with the browser and simulate user interaction with HTML elements or pages. What is Node.js?
But today we’ll be exploring headless Chrome via Puppeteer, as it’s a relatively newer player, released at the start of 2018. Editor’s note: It’s worth mentioning Intoli’s Remote Browser, another new player, but that will have to be a subject for another article. What exactly is Puppeteer?
Open Chromium's Info.plist
(you may find it here node_modules/puppeteer/.local-chromium/mac-XXXXXX/chrome-mac/Chromium.app/Contents/Info.plist
) in an editor and add the following piece after the first <dict>
and before <key>
:
<key>LSBackgroundOnly</key>
<string>True</string>
This works on any OS X application.
Source: Keep applications from stealing focus when opening in OS X
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