0

I would like to extract the following text fields that are located within a g tag which is located in a svg tag (the url: https://www.msci.com/our-solutions/esg-investing/esg-ratings-climate-search-tool). I put in a company name and search for it, expand the last drop down menu and want to extract information from the <svg>.

HTML Part I:

enter image description here

HTML Part II:

enter image description here

I tried what has been suggested here: Extracting text from svg using python and selenium but I did not mange to get it.

My code:

test = driver.find_element(By.XPATH, "//*[local-name()='svg' and @class='highcharts-root']//*[local-name()='g' and @class='highcharts-axis-labels highcharts-xaxis-labels']//*[name()='text']").text
print(test)

1 Answer 1

0

To extract the texts e.g. Sep-18, Nov-22 you have to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR and text attribute:

    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "svg.highcharts-root g.highcharts-axis-labels.highcharts-xaxis-labels text")))])
    
  • Using XPATH and get_attribute("innerHTML"):

    print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[local-name()='svg' and @class='highcharts-root']//*[local-name()='g' and @class='highcharts-axis-labels highcharts-xaxis-labels']//*[local-name()='text']")))])
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant detailed discussions in:

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! the first option worked for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.