python - 存储图像的标题文本 Selenium Webdriver Python

标签 python selenium-webdriver webdriver title

我正在使用 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/

相关文章:

python - 这个简单的 python 代码有什么问题?

python - Python列表中可被 'n'整除的重复元素的最大索引

webdriver - Puppeteer 正在下载无法拦截或停止的未经请求的文件

python - 如何在 WebDriver 测试中更改 Firefox 设置?

java - 当我在 Webdriver 中使用 Java 运行脚本时,我收到错误消息 "No alert is active"

python - 优化 numpy 网格创建以实现高效插值

python - 仅当使用 jax.debug.breakpoint() 时,才会出现 jax.errors.UnexpectedTracerError

java - Selenium : find part of identifier

xml - 用于选择下拉列表中第一个元素的 XPath

selenium - Xpath 有效,可以在 Elements 和 Console 中找到,但在运行应用程序时找不到