Question

I am scraping with python, and I need to save the image title (the text that pops up when you hover over the image). Here is my attempt so far:

from selenium import webdriver
driver= webdriver

imageTitle= driver.find_elements_by_xpath("//td[2]/div/img").title.encode('utf8')

When I try to run the code, I get an AttributeError: 'list' object has no attribute 'title'. I've also tried:

imageTitle= driver.find_elements_by_xpath("//td[2]/div/img").text.encode('utf8')

this just changes to an AttributeError: 'list' object has no attribute 'text'

I'm sure this is a relatively simple fix, but I am just completely lost on how to do this, Thanks

Was it helpful?

Solution

Because you are using find_elements_by_xpath, not find_element_by_xpath, note your one is plural elements, while the other one is element.

driver.find_elements_by_xpath will return a list of elements, text is a property for a single element. You need either use find_element_by_xpath, or index find_elements_by_xpath.

AttributeError: 'list' object has no attribute 'text' has clearly told you that.

Furthermore, the title you mean is the attribute of the element, so need this

imageTitle= driver.find_element_by_xpath("//td[2]/div/img").get_attribute("title")

Here is the API documentation, please read carefully when coding.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top