Feign focus in Selenium chrome browser

本秂侑毒 提交于 2019-12-04 08:26:58

Since the page gets focus information through the onfocus and onblur callbacks in the window object, you can just call them yourself.

browser.execute_script("window.onfocus()")
browser.execute_script("window.onblur()")

For fun, try this script out:

from selenium import webdriver
import time

chromedriver = "./chromedriver"
browser = webdriver.Chrome(executable_path = chromedriver)

browser.get('http://anubiann00b.github.io/FocusTest/')

while True:
    browser.execute_script("window.onfocus()")
    time.sleep(1)
    browser.execute_script("window.onblur()")
    time.sleep(1)

And of course, if you want to make the browser think it's always focused, make the onblur method call the onfocus method:

browser.execute_script("window.onblur = function() { window.onfocus() }")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!