【问题标题】:Storing an image's title text Selenium Webdriver Python存储图像的标题文本 Selenium Webdriver Python
【发布时间】:2014-03-21 16:35:55
【问题描述】:

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

我确信这是一个相对简单的修复,但我完全不知道如何做到这一点,谢谢

【问题讨论】:

    标签: python selenium-webdriver webdriver title


    【解决方案1】:

    因为您使用的是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文档,编码时请仔细阅读。

    【讨论】:

    • 您似乎发现了我的错误,感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-08-03
    • 2016-10-19
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多