I found a span on a website that is not visible and I can’t scrape it! Why?

Currently I’m trying to scrape data from a website. Therefore I’m using Selenium. Everything is working as it should. Until I realised I have to scrape a tooltiptext.

I found already different threads on stackoverflow that are providing an answer. Anyway I did not manage to solve this issue so far.

After a few hours of frustration I realised the following: enter image description here

This span has nothing to do with the tooltip I guess. Because the tooltip looks like this: enter image description here

There is actually a span that I can’t read. I try to read it like this:

bewertung = driver.find_elements_by_xpath('//span[@class="a-icon-alt"]')
for item in bewertung:
    print(item.text)

So Selenium finds this element. But unfortunatly ‘.text’ returns nothing. Why is it always empty ? And what for is the span from the first screenshot ? Btw. it is not displayed at the Website as well.

Answer

Since you’ve mentioned Selenium finds this element, I would assume you must have print the len of bewertung list

something like

print(len(bewertung))

if this list has some element in it, you could probably use innerText

bewertung = driver.find_elements_by_xpath('//span[@class="a-icon-alt"]')
for item in bewertung:
    print(item.get_attribute("innerText"))

Note that, you are using find_elements which won’t throw any error instead if it does not find the element it will return an empty list.

so if you use find_element instead, it would throw the exact error.

Also, I think you’ve xpath for the span (Which does not appear in UI, sometime they don’t appear until some actions are triggered.)

You can try to use this xpath instead:

//i[@data-hook='average-stars-rating-anywhere']//span[@data-hook='acr-average-stars-rating-text']

Something like this in code:

bewertung = driver.find_elements_by_xpath("//i[@data-hook='average-stars-rating-anywhere']//span[@data-hook='acr-average-stars-rating-text']")
    for item in bewertung:
        print(item.text)