发送密钥不是元素,而是一般的selenium

mic*_*imo 9 python selenium webdriver python-2.7 selenium-webdriver

问题:

我想send_keys(Keys.LEFT_CONTROL + 't') 现在这样做我在页面上找到任何元素

elem = self.browser.find_element_by_name('body')
elem.send_keys(Keys.LEFT_CONTROL + 't')
Run Code Online (Sandbox Code Playgroud)

问题是,每次我想发送上面的键时,我必须找到一些元素,实际上我并不感兴趣.

如何一般发送密钥而不是页面的特定对象,我想要一些类似的东西self.browser.send_keys(Keys.LEFT_CONTROL + 't')?它甚至可能吗?

Jam*_*ees 18

您正在使用WebDriver与页面上的实际元素进行交互.不起作用.

尝试使用Actions

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

actions = ActionChains(driver)
actions.send_keys(Keys.LEFT_CONTROL + 't')
actions.perform()
Run Code Online (Sandbox Code Playgroud)

请参阅文档:http: //selenium-python.readthedocs.io/api.html?highlight = send_keys #module-selenium.webdriver.common.action_chains