selenium python send_key 错误:列表对象没有属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29957373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:12:44  来源:igfitidea点击:

selenium python send_key error: list object has no attribute

pythonseleniumkey

提问by rearThing

Helo,

你好,

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

我的 xpath 确实在 firePath 中进行了验证,但是当我尝试发送 _key 时出现错误。

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

userID.send_keys('username')

AttributeError: 'list' object has no attribute 'send_keys'

AttributeError: 'list' 对象没有属性 'send_keys'

Can someone toss me a bone please?

有人可以扔给我一根骨头吗?

采纳答案by Saifur

You are getting a Listof 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_xpathinstead. Refer to thisapi doc.

您正在获得一个webElements列表driver.find_elements_by_xpath(".//*[@id='UserName']")其中当然不是单个元素并且没有send_keys()方法。使用find_element_by_xpath来代替。请参阅api 文档。

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

userID.send_keys('username')

回答by user2661518

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)

当尝试对列表而不是元素执行操作时,会发生此错误。当我尝试单击按钮提交凭据时遇到了类似的错误。我通过模拟键盘上的回车键找到了解决方法。例如,使用 xpath/css 等查找页面上的任何元素并发送回车键。
driver.find_element_by_id('test_id').send_keys(Keys.ENTER)

回答by Dozon Higgs

I had the same problem - so I just did:

我遇到了同样的问题 - 所以我只是做了:

userID[0].send_keys('username')

Worked for me

为我工作

回答by oriolowonancy

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.

我有同样的问题,这对我有用。

回答by sabi Otman

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

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

find_element without s.

find_element 没有s.

回答by Reza_nadimi

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:

或从 find_elements_by_ID 中删除“s”,例如:

userID = driver.find_element_by_ID('username')

userID.send_keys('username')