selenium python send_key error: list object has no attribute

前端 未结 6 1688
花落未央
花落未央 2021-01-19 00:31

Helo,

my xpath does validate in firePath but when I try to send _key I get an error.

userID = driver.find_elements_by_xpath(\".//*[@id=\'UserName\']\         


        
相关标签:
6条回答
  • 2021-01-19 00:57

    driver.find_element_by_xpath(".//*[@id='UserName']").send_keys('username')

    find_element without s.

    0 讨论(0)
  • 2021-01-19 01:00

    This error occurs when one tries to perform action on list instead of element. I was getting similar error when I was trying to click on button to submit credentials. I found the work around by emulating pressing enter key on keyboard. e.g. find any element on page using xpath/css, etc. and send enter key.
    driver.find_element_by_id('test_id').send_keys(Keys.ENTER)

    0 讨论(0)
  • 2021-01-19 01:02

    I had the same problem - so I just did:

    userID[0].send_keys('username')
    

    Worked for me

    0 讨论(0)
  • 2021-01-19 01:09

    Please replace your code with the following code because you like addressing the first component of your list, not whole list.

    userID = driver.find_elements_by_ID('username')[0]
    
    userID.send_keys('username')
    

    or delete "s" from find_elements_by_ID such as follow:

    userID = driver.find_element_by_ID('username')
    
    userID.send_keys('username')
    
    0 讨论(0)
  • 2021-01-19 01:14

    instead of this:

    userID = driver.find_elements_by_xpath(".//*[@id='UserName']")
    
    userID.send_keys('username')
    

    try:

    userID = driver.find_element_by_xpath(".//*[@id='UserName']")
    
    userID.send_keys('username')
    

    I had the same issues and that worked for me.

    0 讨论(0)
  • 2021-01-19 01:22

    You are getting a List of webElements with driver.find_elements_by_xpath(".//*[@id='UserName']") which of course not a single element and does not have send_keys() method. use find_element_by_xpath instead. Refer to this api doc.

    userID = driver.find_element_by_xpath(".//*[@id='UserName']")
    
    userID.send_keys('username')
    
    0 讨论(0)
提交回复
热议问题