简体   繁体   English

无法发送密钥 selenium webdriver python

[英]Can't send keys selenium webdriver python

Trying to execute simple test尝试执行简单的测试

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

And getting the error并得到错误

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'

What's the problem?有什么问题?

WebDriver instance does not have send_keys() method . WebDriver实例没有send_keys()方法 That's what the error is actually about:这就是错误的实际含义:

'WebDriver' object has no attribute 'send_keys' 'WebDriver' 对象没有属性 'send_keys'

Call send_keys() on a WebElement instance which is returned by find_element_by_*() methods - find_element_by_name() in your case:在由find_element_by_*()方法返回的WebElement实例上调用send_keys() - 在您的情况下为find_element_by_name()

element = driver.find_element_by_name('q')
element.send_keys("hey")

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.仅供参考,还有一个ActionChains,它有助于构建动作链或应用更复杂的动作,如拖放或鼠标移动。 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()

You must change the reference element您必须更改参考元素

driver.get('http://google.com')
elem.find_element_by_name('q')
elem.send_keys('hey')

Did you try changing your reference element.您是否尝试更改参考元素。 It would be a good practice if you call webdriver using different reference.如果您使用不同的引用调用 webdriver,这将是一个很好的做法。 Your code after changing reference.更改参考后的代码。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Firefox()
driver.get('http://google.com')
#Firefox Webdriver is created and navigated to google.

elem = driver.find_element_by_name('q')
elem.send_keys('hey',Keys.RETURN)
#Keys.RETURN = Pressing Enter key on keyboard

time.sleep(5)
driver.close()

I had faced same issue, but I got solution for that.我遇到了同样的问题,但我得到了解决方案。

Use move_to_element in ActionChains .使用move_to_elementActionChains

  1. Find element.查找元素。 elem = driver.find_element_by_*

  2. create your actionchain driver actions = ActionChains(driver)创建您的actions = ActionChains(driver)链驱动程序actions = ActionChains(driver) ActionChains actions = ActionChains(driver)

  3. use move to command, so webdriver will point to that element's position, but before you send key, you need to set webdriver position on that element by using click() function.使用 move to 命令,因此webdriver指向该元素的位置,但在发送密钥之前,您需要使用click()函数在该元素上设置webdriver位置。 Now, webdriver has a surface area to place given keys (data) and for that you will use send_keys() function.现在, webdriver有一个表面区域来放置给定的键(数据) ,为此您将使用send_keys()函数。 In the last just put perform() function to execute these all task very smoothly.在最后只是放置perform()函数来非常顺利地执行这些所有任务。 actions.move_to_element(<ELEMENT>).click().send_keys(<DATA>).perform()

    https://selenium-python.readthedocs.io/api.htmlt https://selenium-python.readthedocs.io/api.htmlt

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

driver = webdriver.Firefox()

driver.get('http://google.com')
elem = driver.find_element_by_name('q')
actions = ActionChains(driver)
actions.move_to_element(elem).click().send_keys('hey').perform()
The problem with your code is you have not tell driver where to send the keys 

    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') // driver dont know where to send the keys

correct code would be :

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

driver = webdriver.Firefox()

driver.get('http://google.com')
goog = driver.find_element_by_name('q') // you have stored element in goog variable
goog.send_keys('hey') // you told variable to send hey 

or 

you can directly send after finding the element like :

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').send_keys('hey')

If it helps so please accept the answer !!

Its with the version of selenium which is causing the issue.它与导致问题的硒版本。 I faced the same issue.我遇到了同样的问题。

Its with the selenium version 3.3.3 which has the compatibility problem.它与具有兼容性问题的硒版本 3.3.3。

Try: pip uninstall selenium pip install selenium==3.3.1尝试: pip 卸载硒 pip install selenium==3.3.1

Hope it works.希望它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM