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

Time:07-26

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

This is the website: https://simplywall.st/stocks/us/energy/nyse-hal/halliburton#valuation The element: Industry Avg 1.1x

I really don't konw how to address to that element in order to get the text.

Image of the graph with the label with the needed information

I hope somebody can help.

Thanks a lot,

Christian

CodePudding user response:

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
  • Related