Actionchains perform not working when driver loses focus

萝らか妹 提交于 2019-12-11 23:45:45

问题


I have a code that basically triggers a browser shortcut using actionchains. I'm using Chrome driver 2.27 and running python 3.6 through Jupyter notebook(though that shouldn't matter). The problem is that if the code runs along with the creation of the webdriver instance it works and shows the download bar.

Instead if I run the code to create the webdriver, open the new window, minimize it and then try running the code to trigger the shortcut it doesn't work. I'm guessing it has something to do with the driver losing focus because I manually peek at the new window created.

Code to create the webdriver

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
chromedriver= "chromedriver_v2.27.exe"
driver = webdriver.Chrome(chromedriver)
driver.get("https://www.google.com")  

Code to trigger the browser shortcut

ActionChains(driver).key_down(Keys.CONTROL).send_keys('j').key_up(Keys.CONTROL).perform()

回答1:


ActionChains won't work if the browser window is minimized. As a workaround you can call driver.maximize() before you execute the Action chain to send keyboard shortcut to the window.

driver.maximize()
ActionChains(driver).key_down(Keys.CONTROL).send_keys('j').key_up(Keys.CONTROL).perform()


来源:https://stackoverflow.com/questions/50713695/actionchains-perform-not-working-when-driver-loses-focus

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