已解决AttributeError: ‘list‘ object has no attribute ‘text‘
已解决AttributeError: ‘list’ object has no attribute ‘text’
报错问题
粉丝群里面的一个小伙伴遇到问题跑来私信我,想用selenium操作浏览器自动化,但是发生了报错(当时他心里瞬间凉了一大截,跑来找我求助,然后顺利帮助他解决了,顺便记录一下希望可以帮助到更多遇到这个bug不会解决的小伙伴),报错代码如下所示:
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222") # 通过端口号接管已打开的浏览器
driver = webdriver.Chrome(options=chrome_options)
print(driver.title)
print(driver.find_elements(by=By.CLASS_NAME, value="nc-lang-cnt").text)
可以看到代码中也有高亮:
报错信息内容截图如下所示:
报错翻译
报错信息内容翻译如下所示:
属性错误:“list”对象没有属性“text”
报错原因
报错原因:
后经仔细查看,是因为driver.find_elements(by=By.CLASS_NAME, value="nc-lang-cnt").text
多了一个“s
”,因为:find_elements
返回 WebElement 的列表,如下图所示:
而.text
函数 (和其他方法)只能接受单个 WebElement,所以报错。
解决方法
所以应该将find_elements
修改为find_element
即可:
print(driver.find_element(by=By.CLASS_NAME, value="nc-lang-cnt").text)
以上是此问题报错原因的解决方法,欢迎评论区留言讨论是否能解决,如果有用欢迎点赞收藏文章谢谢支持,博主才有动力持续记录遇到的问题!!!
千人全栈VIP答疑群联系博主帮忙解决报错
由于博主时间精力有限,每天私信人数太多,没办法每个粉丝都及时回复,所以优先回复VIP粉丝,可以通过订阅限时9.9付费专栏《100天精通Python从入门到就业》进入千人全栈VIP答疑群,获得优先解答机会(代码指导、远程服务),白嫖80G学习资料大礼包,专栏订阅地址:https://blog.csdn.net/yuan2019035055/category_11466020.html
-
优点:作者优先解答机会(代码指导、远程服务),群里大佬众多可以抱团取暖(大厂内推机会),此专栏文章是专门针对零基础和需要进阶提升的同学所准备的一套完整教学,从0到100的不断进阶深入,后续还有实战项目,轻松应对面试!
-
专栏福利:简历指导、招聘内推、每周送实体书、80G全栈学习视频、300本IT电子书:Python、Java、前端、大数据、数据库、算法、爬虫、数据分析、机器学习、面试题库等等
-
注意:如果希望得到及时回复,和大佬们交流学习,订阅专栏后私信博主进千人VIP答疑群
相关文章
- 解决At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JAR
- [Immutable.js] Differences between the Immutable.js Map() and List()
- List 与以逗号分割String类型相互转换
- 更新yum源却忘了生成缓存 造成每次启动机器报:the package list needs to be rebuilt
- 重学c#系列——list(十二)
- 【RF库Collections测试】Sort List
- Enterprise search - Build Search dropdown list - cache issue
- paip.hibernate list 返回位null的解决
- List,Set,Map存取元素各有什么特点
- 【Codeforces Round #432 (Div. 1) B】Arpa and a list of numbers
- 如何从 SAP Fiori Elements List Report Table 点击事件响应函数里拿到表格某一行的信息
- SAP Fiori Elements list report filter - implemented by framework
- 成功解决ValueError: ‘usecols‘ must either be list-like of all strings, all unicode, all integers or a ca
- 成功解决TypeError: sequence item 0: expected str instance, list found
- 成功解决AttributeError: 'list' object has no attribute 'ndim'
- 成功解决AttributeError: 'list' object has no attribute 'ndim'
- 已解决TypeError:List indices must be integers or slices , not str
- java笔记:流式编程 数组与List集合互转
- 解决 java.sql.SQLSyntaxErrorException:Unknown column ‘xxx‘ in ‘field list‘的问题
- Groovy sort() list
- Redis源码剖析--列表t_list实现
- Redis 与 list 相关的常用命令
- MySQL ---- In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated