我正在使用 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'
已经清楚地告诉你了。
另外,你说的title是元素的属性,所以需要这个
imageTitle= driver.find_element_by_xpath("//td[2]/div/img").get_attribute("title")
Here是API文档,请在编码时仔细阅读。
关于python - 存储图像的标题文本 Selenium Webdriver Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22564396/