简体   繁体   English

如何修复 Selenium Traceback(最近一次调用最后一次):print(contact.text()) AttributeError: 'list' object has no attribute 'text'

[英]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:要么执行以下操作:

  1. use find_element instead.使用find_element代替。

Like:像:

contact = driver.find_element_by_class_name('_3q9s6')
print(contact.text)
  1. 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.

相关问题 回溯(最近一次调用最后一次):文件“ <pyshell#439> “,第3行,in <module> print(l.text)AttributeError:&#39;list&#39;对象没有属性&#39;text&#39; - Traceback (most recent call last): File “<pyshell#439>”, line 3, in <module> print(l.text) AttributeError: 'list' object has no attribute 'text' AttributeError: &#39;list&#39; 对象没有属性 &#39;_sa_instance_state&#39; Traceback(最近一次调用最后) - AttributeError: 'list' object has no attribute '_sa_instance_state' Traceback (most recent call last) 追溯(最近一次呼叫最近):AttributeError:文件“”,第1行,模块“ socket”没有属性“ close” - Traceback (most recent call last): File “”, line 1, in AttributeError: module 'socket' has no attribute 'close' Selenium AttributeError: 'list' object 没有属性 'text' - Selenium AttributeError: 'list' object has no attribute 'text' Python Selenium Traceback(最近一次调用最后一次): - Python Selenium Traceback (most recent call last): 我该如何解决这个 AttributeError: 'NoneType' object has no attribute 'text'? - How do i fix this AttributeError: 'NoneType' object has no attribute 'text'? 如何修复错误:AttributeError: 'str' object has no attribute 'text' - How to fix error: AttributeError: 'str' object has no attribute 'text' 如何解决“ AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39;”的问题 - How to fix “AttributeError: 'NoneType' object has no attribute 'text'” for <span> 如何修复 AttributeError:ResultSet 对象没有属性“get_text” - How to fix AttributeError: ResultSet object has no attribute 'get_text' 如何修复AttributeError:当我尝试替换csv读取中的某些文本时,“列表”对象没有属性“替换” - How to fix AttributeError: 'list' object has no attribute 'replace' when I try to replace some text in a csv read
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM