我正在使用python进行抓取,并且需要保存图像标题(将鼠标悬停在图像上时会弹出的文本)。到目前为止,这是我的尝试:
from selenium import webdriver
driver= webdriver
imageTitle= driver.find_elements_by_xpath("//td[2]/div/img").title.encode('utf8')
当我尝试运行代码时,出现一个AttributeError: 'list' object has no attribute 'title'
。我也尝试过:
imageTitle= driver.find_elements_by_xpath("//td[2]/div/img").text.encode('utf8')
这只是变成一个 AttributeError: 'list' object has no attribute 'text'
我敢肯定这是一个相对简单的修复,但是我完全不知道该怎么做,谢谢
因为您使用的find_elements_by_xpath
不是find_element_by_xpath
,请注意您的一个是复数elements
,而另一个是element
。
driver.find_elements_by_xpath
将返回元素列表,text
是单个元素的属性。您需要使用find_element_by_xpath
或索引find_elements_by_xpath
。
AttributeError: 'list' object has no attribute 'text'
清楚地告诉过你。
此外,您所指的标题是元素的属性,因此需要
imageTitle= driver.find_element_by_xpath("//td[2]/div/img").get_attribute("title")
这是API文档,请在编码时仔细阅读。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句