9

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',
            ]
avocado
  • 2,615
  • 3
  • 24
  • 43

2 Answers2

17

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

mbit
  • 2,763
  • 1
  • 10
  • 16
  • I actually use Chrome and a custom user_data folder, and I added the above key to the Info.plist (inside user_data folder), doesn't work – avocado Mar 08 '20 at 18:22
  • 2
    @avocado you should add it to Chrome then, find chrome's info.plist which is probably at `path/to/Chrome.app/Contents/Info.plist`, not user_data folder. – mbit Mar 08 '20 at 19:05
  • 2
    any way to do this for linux? – Aryeh Armon Dec 30 '20 at 12:41
  • 4
    how to do it on Windows? – ming Oct 08 '21 at 08:42
  • I call the Chrome Canary executable on Mac, and added this into the .app/Contents/Info.plist, but it still captures focus. – John Targaryen Feb 17 '23 at 21:12
  • Related discussion on github.com/puppeteer with possible Windows solution, https://github.com/puppeteer/puppeteer/issues/6606 – here Apr 13 '23 at 02:42
-2

You can use: headless: true.

I did this:

const browser = await puppeteer.launch({
    headless: true,
    executablePath: 'C:/Users/User/Desktop/Chrome-win/chrome.exe'
});
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
AlexPapa
  • 1
  • 1
  • Please write your answer in English, as Stack Overflow is an [English-only site](//meta.stackoverflow.com/a/297680). – mozway Jun 03 '23 at 17:37
  • 1
    I surely know what headless looks like, but this is not the solution I wanted – avocado Jun 04 '23 at 06:48
  • Please read "[answer]" and "[Explaining entirely code-based answers](https://meta.stackoverflow.com/q/392712/128421)". It helps more if you supply an explanation why this is the preferred solution and explain how it works. We want to educate, not just provide code. – the Tin Man Jun 05 '23 at 23:05
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34492616) – Wahlstrommm Jun 07 '23 at 11:38