Can't send keys selenium webdriver python

后端 未结 4 771
庸人自扰
庸人自扰 2021-01-21 08:16

Trying to execute simple test

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

driver = webdriver.Firefox()

driver.get(\'http://g         


        
相关标签:
4条回答
  • 2021-01-21 08:35

    You must change the reference element

    driver.get('http://google.com')
    elem.find_element_by_name('q')
    elem.send_keys('hey')
    
    0 讨论(0)
  • 2021-01-21 08:37

    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.

    Try: pip uninstall selenium pip install selenium==3.3.1

    Hope it works.

    0 讨论(0)
  • 2021-01-21 08:40

    WebDriver instance does not have send_keys() method. That's what the error is actually about:

    'WebDriver' object has no attribute 'send_keys'

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

    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()
    
    0 讨论(0)
  • 2021-01-21 08:51

    Did you try changing your reference element. It would be a good practice if you call webdriver using different reference. 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()
    
    0 讨论(0)
提交回复
热议问题