Extract list of text from g element within svg tag using Selenium and Python

283 views Asked by At

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

There are 1 answers

1
undetected Selenium On BEST ANSWER

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: