首页 > 解决方案 > 如何修复 Selenium Traceback(最近一次通话最后一次):打印(contact.text())AttributeError:'list'对象没有属性'text'

问题描述

我正在创建 Whatsapp 脚本,它将我所有的 Whatsapp 联系人姓名保存在 Selenium 中。

错误:

Traceback (most recent call last):
  File ".\main.py", line 11, in <module>
    print(contact.text())
AttributeError: 'list' object has no attribute 'text'

代码:

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

谢谢

标签: pythonseleniumselenium-webdriver

解决方案


find_elements 

list在 Selenium-Python 绑定中返回 a 。因此,您不能.text在列表中使用。

执行以下操作:

  1. 改为使用find_element

喜欢 :

contact = driver.find_element_by_class_name('_3q9s6')
print(contact.text)
  1. 如果您想使用find_elements,请执行以下操作:

    contact = driver.find_elements_by_class_name('_3q9s6')
    for c in contact:
       print(c.text)
    

推荐阅读