python - 如何修复 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()
谢谢
解决方案
find_elements
list
在 Selenium-Python 绑定中返回 a 。因此,您不能.text
在列表中使用。
执行以下操作:
- 改为使用
find_element
。
喜欢 :
contact = driver.find_element_by_class_name('_3q9s6')
print(contact.text)
如果您想使用
find_elements
,请执行以下操作:contact = driver.find_elements_by_class_name('_3q9s6') for c in contact: print(c.text)
推荐阅读
- foreign-keys - 从查找字段中获取用户友好的值
- javascript - 使用 Javascript 验证表单输入
- javascript - 如何在 TypeScript 中导入 JavaScript 模块并转换为 TypeScript 命名空间
- ruby-on-rails-5 - 如何在部署中更新 Rails 5.2 凭据和主密钥
- python - 使用 Python setup.py 通过 develop 与 install 安装不同的依赖项
- sql - 添加两个查询一起使用
- python - 使用 Python 提取准确的价格数据,如在 Excel 表中看到的那样,而不用四舍五入 Python 端的值
- multithreading - Async/Await 仍然阻塞 UI?
- c - 为什么我会收到“free(): invalid pointer”的错误?
- django - 在自己的服务器上托管 Django Rest 框架