← Back to Discord Forum

window.onblur, window.onfocus no firing in page.evaluate

The issue what I'm facing is onblur, onfocus event not firing but when I manually open the chrome dev tools console of browser then it works as expected. // ===========CODE==================== const page = await browserContexts.pages()[0]; await page.goto('https://www.hbo.com', { waitUntil: 'domcontentloaded' }); await page.evaluate(() => { window.onblur = () => { console.log('blur'); window.location.href = 'https://www.abc.com' }; window.onfocus = () => { console.log('focus'); }; }); setTimeout(async () => { const page2 = await browserContexts.newPage(); await page2.goto('https://google.com') }, 5000); // ===========CODE==================== Expectation if new tab is created then it should trigger window.onblur but currently its not happening. But works when I manually open chrome dev tools first then create new tab or move to another tab Its happening for both evaluate as well as addInitScript

Also tried below code in evaluate but not working

window.addEventListener('blur', () => { console.log('blur'); window.location.href = 'https://www.abc.com' }); window.addEventListener('focus', () => { console.log('focus'); });

And also visibilitychange event also not working even if chrome dev tools is opened.

This thread is trying to answer question "Why are window.onblur and window.onfocus events not firing in page.evaluate method unless the Chrome dev tools console is manually opened?"

5 replies

Hi, Playwright does not need a page/tab/window to be focused to automate/control it.

If you do want to focus a page, there is a page.focus() method, but I see it's discouraged. Instead you're advised to use Locator.focus() on an element in the new page/tab/window.

I suspect when you do that, the blur/focus events on the window will happen as expected.


Oops, I looked at the wrong method. I think bringToFront is what you need: https://playwright.dev/docs/api/class-page#page-bring-to-front

Thank you for answering really appreciated. Well I already tried page.focus() & page.blur() both depricated methods but not working.

Logically bringToFront should trigger window.onfocus method because it activates my previous or specified page tab but still it didn't fire my window.onfocus() event.

I'm still not able to understand why it triggers onblur and onfocus events only when I open my browser console manually.

From my observation, I think playwright not able understand if the page is still in visible state or not which causes not to trigger onfocus or onblur events and when I open my browser console manually then somehow it gets that information correctly.


Sounds like a good candidate for a GitHub issue.

Yeah I’ll raise issue for this although There’s a workaround that I can programmatically do is use dispatchEvent in evaluate method which forces to trigger these events

Related Discord Threads

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.