我已经尝试了几种不同的方法,但都没有成功。
Switching to a window that does not exist with hopes that it would then open a new window upon failure to locate said window:
driver.switch_to_window(None)
Iterating through open windows (although there is currently only one)
for handle in driver.window_handles: driver.switch_to_window(handle)
Attempting to simulate a keyboard key press
from selenium.webdriver.common.keys import Keys driver.send_keys(Keys.CONTROL + 'T')
driver.find_element_by_id('elementID').send_keys(Keys.CONTROL + 'T')
然而,当像这样的命令被发送到一个元素时,它似乎什么都没有发生。我试图找到页面上最顶层的HTML元素并将键盘输入发送到那里,但又遭遇了失败:
driver.find_element_by_id('wrapper').send_keys(Keys.CONTROL + 'T')
我在网上找到了另一个版本,但由于不确定需要导入哪个类/模块,无法验证其有效性或无效性。
act = ActionChains(driver)
act.key_down(browserKeys.CONTROL)
act.click("").perform()
act.key_up(browserKeys.CONTROL)
这两种语法非常相似(我不确定哪一种或两种都是正确的语法)
actions.key_down(Keys.CONTROL)
element.send_keys('t')
actions.key_up(Keys.CONTROL)
execute_script()
。如果您需要传递项目/cookie,请创建一个target=_blank链接,然后单击它即可。 - m3nda