当驾驶员失去注意力时,动作链无法正常工作 [英] Actionchains perform not working when driver loses focus

查看:70
本文介绍了当驾驶员失去注意力时,动作链无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,该代码基本上使用动作链触发浏览器快捷方式.我正在使用Chrome驱动程序2.27并通过Jupyter笔记本运行python 3.6(尽管没关系).问题是,如果代码与Webdriver实例的创建一起运行,它将起作用并显示下载栏.

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.

相反,如果我运行代码来创建Web驱动程序,请打开新窗口,将其最小化,然后尝试运行代码以触发不起作用的快捷方式.我猜想这与驱动程序失去焦点有关,因为我手动浏览了创建的新窗口.

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()

如果将浏览器窗口最小化,

推荐答案

ActionChains将不起作用.解决方法是,可以在执行操作链之前将driver.maximize()调用,以将键盘快捷键发送到窗口.

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()

这篇关于当驾驶员失去注意力时,动作链无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆