无法发送密钥 selenium webdriver python [英] Can't send keys selenium webdriver python
问题描述
尝试执行简单的测试
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('http://google.com')
driver.find_element_by_name('q')
driver.send_keys('hey')
并得到错误
Traceback (most recent call last):
File "C:/webdriver/test.py", line 8, in <module>
driver.send_keys('hey')
AttributeError: 'WebDriver' object has no attribute 'send_keys'
有什么问题吗?
推荐答案
WebDriver
实例没有 send_keys()
方法.这就是错误的真正含义:
WebDriver
instance does not have send_keys()
method. That's what the error is actually about:
'WebDriver' 对象没有属性 'send_keys'
'WebDriver' object has no attribute 'send_keys'
在 WebElement
实例,由 find_element_by_*()
方法返回 - find_element_by_name()
在您的情况下:
Call send_keys()
on a WebElement
instance which is returned by find_element_by_*()
methods - find_element_by_name()
in your case:
element = driver.find_element_by_name('q')
element.send_keys("hey")
<小时>
仅供参考,还有一个ActionChains
类 有助于构建动作链或应用更复杂的动作,如拖放或鼠标移动.在这种情况下,这是一个开销,但仅举个例子:
And just FYI, there is also an ActionChains
class which is useful do build up chains of actions or apply more complex actions like drag&drop or mouse move. It's an overhead in this case, but just for the sake of an example:
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.move_to_element(element).send_keys("hey").perform()
这篇关于无法发送密钥 selenium webdriver python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!