I can't read the text embedded in a graph label via selenium

Question:

I don’t know how to read the text inside the label: "Industry Avg 1.1x".

This is the website: https://simplywall.st/stocks/us/energy/nyse-hal/halliburton#valuation
The element: <text fill="#1B222D" font-size="13" transform="translate(-111 0)">Industry Avg 1.1x</text>

I really don’t know how to address to that element in order to get the text.

Image of the graph with the label with the needed information:

figure 1

I hope somebody can help.

Thanks a lot,

Christian

Asked By: Christian Tovazzi

||

Answers:

That label doesn’t appear until you’ve scrolled it into view. I think the easiest way is to scroll all the way to the bottom, at which point the element can be found. It’s tricky though because you have to scroll "like a user would". To do that, I believe the following should work, although it is far from optimized:

last_height = driver.execute_script("return document.body.scrollHeight")
    while True:
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight-1000);")
        # Wait to load the page.
        driver.implicitly_wait(3) # seconds
        new_height = driver.execute_script("return document.body.scrollHeight")
    
        if new_height == last_height:
            break
        last_height = new_height
        driver.implicitly_wait(3) # seconds

Probably scratch the above. I found you can identify the section surrounding the element and click it to make your element appear. Try this:

driver.find_element_by_css_selector('section[data-cy-id=report-sub-section-price-to-earnings-ratio-vs-industry]').click()

Then, to identify the element and get the text, try the following:

text_you_want = driver.find_elements_by_css_selector('g > text[font-size="13"]')[1].innerHTML
Answered By: C. Peck
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.