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

Issue

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

Solution

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

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published