puppeteer: Puppeteer not working when is not focused
Environment:
- Puppeteer version: 1.8.0
- Platform / OS version: Win 10 64b and Ubuntu 18.04.1 LTS
- URLs (if applicable): No
- Node.js version: v10.6.0
I am using puppeteer for testing my pages. When I run the app and hide it into background then puppeteer is paused (or freeze). When I move puppeteer into foreground is running normally.
I think that its problem “when window is not focused, its not working”.
This behavior is on windows and linux too.
Set up code:
const args = []
args.push('--start-maximized')
args.push('--disable-gpu')
args.push('--disable-setuid-sandbox')
args.push('--force-device-scale-factor')
args.push('--ignore-certificate-errors')
args.push('--no-sandbox')
this.driver = await puppeteer.launch({
headless: false,
args,
userDataDir: `puppeteer_profile`,
})
i happen only “sometimes” I don’t know when.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 28
- Comments: 81 (11 by maintainers)
Nope you are not only one, I have same problem above.
[Solution]:
Add the following values to the array you assign to the args property in the options object. Now, you should be able to run Puppeteer while your window is in the foreground, background, or minimized.
Update: I’m running parallel instances of Puppeteer using the same script in multiple windows with one tab per window. Each window completed successfully whether it was in the foreground, background, or minimized. And, it worked in both headless and headful mode.
Solution and explanation available in an article written by Rohit Kumar, @rkr090 at Big Binary:
Debugging failing tests in puppeteer because of background tab
@JaLe29 This is probably related to https://github.com/WICG/web-lifecycle/
The easy workaround would be:
This should unfreeze javascript execution in the background page.
Have the same issue… the year is 2021 and 3 years had pasted since the problem declared… any solution to this?
Also having this problem i.e. with multiple tabs open only the tab that has focus continues to work properly. Pages without focus “stop running” and ‘.waitForSelector’ calls time out. Very keen for a fix.
I also faced the same problem. I tried the snippet given by @aslushnikov , but it didn’t work. I read in a report where they advised running on multiple instances of browser instead of trying to manage different tabs and it worked for me. It does not pause in separate browser instances. So, if you are stuck and your requirement allows you to use/test multiple browser instances, I think you should go for it.
same issue… puppeteer script paused when switch to another tab, i need multi tabs to work simultaneously. but i have yet to find good solution now. help!!!
same issue… i really need a solution
This is the browser’s implementation that certain things are paused when the tab is inactive. We don’t plan changing this as it matches to how the browser will behave for the end user. You can use the old headless mode that has a different implementation for this feature. For the new headless and headful, you need to activate the tab by calling
page.bringToFront()
to avoid various kinds of throttling. We currently do disable some throttling if there are flags that are exposed for that but usingpage.bringToFront()
is a more reliable way.I also have this problem. If you minimize the browser window, the script pauses
I’m having somewhat a similar problem. I’m launching 5 Chrome 73.0.3683.103 instances (not chromium) on Centos 7. I launch them as child processes so they all run concurrently no problem.
If I don’t minimize them, they run fine even if they are not focused. If I run a script to miminize the windows, they freeze execution until I unminimize. I’m not sure if this is website specific or a problem with chrome/puppeteer. I don’t have time to test more extensively, just wanted to share my experience.
@aslushnikov I tested your solution
But it not working too. Still same behaviour. 😦
Thanks. It’s worked for me!
Ok, just updated to puppeteer 19.x the issue does not happen anymore seems fixed.
Its been almost 3 years and still no luck!!
Same issue with around 45 browser instances parallel open. The three Chrome command line arguments are not solving the issue.
@aslushnikov when we have multiple pages, will adding this snippet make all of them focused? Also it’s
page.target()
as documented. I tried with the snippet,and the html code,
The result,
It didn’t focus back. Should it focus back?
[Solution]
Hi everybody,
I am currently creating an automation that requires multithreading and had this same exact problem. Read through the entire thread and tested several solutions until I was able to fix it. Their was ONE that actually worked for me and was the following:
Inside of each page that is being opened on a different tab, this code should be invoked:
---- UNFREEZE -------
await this.waitForSelector(.......
I had several tabs executing at the same time and all of them got unfreeze by the bringToFront event before actually waiting for a specific selector. This has worked for me, hope this helps
J
+1 with the same problem😭
Sorry to say @fgroupindonesia but still no luck with all of these flags…
+1
I have to run 100+ puppeteer windows (not tabs) in parallel with
puppeteer.launch()
. If I have to use this workaround on all 100 windows, it will be a disaster for me. 😦Still having this issue on puppeteer 20
THis issue still happens on Windows, I have facing it right now
I’m facing same issue, I’m using like this solution for that (solved for me) 🤣 like @jtourisNS
*you need create
delay()
function by your self .+1 same issue.
Multiple browsers, all headless, works for me! I’ve only got 4 pages though so good luck with >10 haha. Hint:
Promise.all
is your friend 😄If possible please use Headless=true, this would launch the browser without UI and tabs losing focus won’t be a problem anymore. ^ @azazrehman
Also still experiencing this issue. Any amount of flags passed to chrome have absolutely no effect (on Microsoft Windows). Will try to reproduce a minimal working example. We have 8 windows with 1-10 tabs each open at all times. Sometimes tabs load indefinitely, sometimes the entire page freezes, as well as puppeteer script execution. Quite often the program will appear to be idle and then open tens of tabs that have apparently been waiting for the newpage command to resolve open at once (usually after clicking into the window, but clicking on the commandline window seems to trigger this as well sometimes). This is incredibly hard to debug, as there seems to be no clear regularity to the issue.
it works on headless : false mode, but if i use headless: true, this problem would happen
I am suffering the same issue. Any fix or workaround?
@ganyanchuan1989 I am using
puppeteer.connect
with headless: false. Its still not working for me.Thanks for that clarification. I can see how it could be considered a patch or workaround for your situation. But, since the OP didn’t mention multiple pages, maybe it could still serve as as a solution for others.
Also, the patch or workaround does allow the script to execute while the window is not focused.
This is happening to me as well. Specifically,
page.type()
will pause until the non-headless Chrome window regains focus. I have a cron which runs every 10 minutes, and if I minimize or switch windows, the typed text will not complete until I restore the window.Hey @aslushnikov I have tested the script you provided above and heartbits do not stop. They do keep happening.
But when I try running normal script, it goes into the paused state and nothing happens on that website page. Things also don’t get timed out from puppeteer’s side. It’s as if the whole puppeteer script and browser just went into a paused state.
I read the WICG/page-lifecycle which goes into more details about the frozen state. But then how come this only happens when I am trying to execute something via puppeteer but not in normal chrome. Or is the argument that it happens in normal execution as well but we don’t see it because we are not actively trying to do anything in that browser.
Shouldn’t this be treated as bug for chromium though. If script is trying to do something in the current page, then that page should never get into the frozen state. If this is already an open bug, could someone link it please. Thanks!
Also, this didn’t help: https://github.com/GoogleChrome/puppeteer/issues/3339#issuecomment-426907894