-1

I tryed find element into tree DOM but I have a problem. I wrote:

age = browser.find_elements_by_xpath("//div[@id='ads']/ul/li[6]/span[2]").text

But all the time it display my the error:

AttributeError: 'list' object has no attribute 'text'

What is wrong?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Kloc5456
  • 37
  • 1
  • 4

2 Answers2

1

If there is just one element that you wanna get the text from use find_element instead of find_elements

age = browser.find_element_by_xpath("//div[@id='ads']/ul/li[6]/span[2]").text

find_elements will give you a list of elements, to get the text of each element you have to loop over the result:

for element in browser.find_elements_by_xpath("//div[@id='ads']/ul/li[6]/span[2]"):
    element.text
HedgeHog
  • 22,146
  • 4
  • 14
  • 36
-1

find_elements* returns a list where is text is an attribute of a WebElement.


Solution

Instead of find_elements_by_xpath() you need to use find_element_by_xpath(). Effectively your line of code will be:

age = browser.find_element_by_xpath("//div[@id='ads']/ul/li[6]/span[2]").text
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352