[英]How To Fix Selenium Traceback (most recent call last): print(contact.text()) AttributeError: 'list' object has no attribute 'text'
I Am Creating Whatsapp Script Which It Saves All My Whatsapp Contacts Name In Selenium.我正在创建 Whatsapp 脚本,它将我所有的 Whatsapp 联系人姓名保存在 Selenium 中。
Error:错误:
Traceback (most recent call last):
File ".\main.py", line 11, in <module>
print(contact.text())
AttributeError: 'list' object has no attribute 'text'
Code:代码:
from selenium import webdriver
from selenium.webdriver.common import keys
import time
driver = webdriver.Firefox(executable_path="geckodriver.exe")
driver.get("https://web.whatsapp.com/")
input("Qr Code Done? : ")
driver.implicitly_wait(10)
contact = driver.find_elements_by_class_name('_3q9s6')
print(contact.text()
Thanks谢谢
find_elements
returns a list
in Selenium-Python bindings.返回 Selenium-Python 绑定中的
list
。 So, you can not use .text
on a list.因此,您不能在列表中使用
.text
。
Either do the following:要么执行以下操作:
find_element
instead.find_element
代替。 Like:像:
contact = driver.find_element_by_class_name('_3q9s6')
print(contact.text)
If you wanna use find_elements
, then do this:如果您想使用
find_elements
,请执行以下操作:
contact = driver.find_elements_by_class_name('_3q9s6') for c in contact: print(c.text)
find_element_by_class_name
is on plural, it finds all of the element s , try: find_element_by_class_name
是复数,它找到所有元素s ,尝试:
contact = driver.find_element_by_class_name('_3q9s6')
print(contact.text)
You where only a character off, close enough:-)你只有一个字符关闭,足够接近:-)
.find_element_*
(without s
) for finding single element, or this is matching the first element if in the web page there are multiple element with same specific: .find_element_*
(没有s
)用于查找单个元素,或者如果在 web 页面中有多个具有相同特定的元素,则这是匹配第一个元素:
contact = driver.find_element_by_class_name('_3q9s6')
print(contact.text)
But your code, .find_elements_*
(with s
), it will return a list.但是您的代码
.find_elements_*
(带有s
)将返回一个列表。 You can achieve with this by accessing the index:您可以通过访问索引来实现:
contact = driver.find_elements_by_class_name('_3q9s6')
#first element
print(contact[0].text)
Note, to grab the text it should .text
not .text()
请注意,要获取文本,它应该是
.text
而不是.text()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.