Home > OS >  get locator from a hover element
get locator from a hover element

Time:11-09

I want to get the locator of this element (5,126, 601) but seem cant get it normally.

I think it will have to hover the mouse to the element and try to get the xpath but still I cant hover my mouse into it because it an SVG element . Any one know a way to get the locator properly?
here is the link to the website: enter image description here

CodePudding user response:

Well, this element is updated only by hovering over the chart.
This is the unique XPath locator for this element:

"//*[name()='text']//*[name()='tspan' and(contains(@style,'bold'))]"

The entire Selenium command can be:

total_text =  driver.find_element(By.XPATH, "//*[name()='text']//*[name()='tspan' and(contains(@style,'bold'))]").text

This can also be done with this CSS Selector: text tspan[style*='bold'], so the Selenium command could be

total_text =  driver.find_element(By.CSS_SELECTOR, "text tspan[style*='bold']").text

Well, CSS Selector looks much shorter :)

CodePudding user response:

Clicking on each node in turn will lead to the accompanying text being placed in the highcharts-label element. This text can then be retrieved and the Quarter (1st tspan) be linked to the Total value (4th tspan) that you desire.

url="https://fundingsocieties.com/progress"
driver.get(url)
chart = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "//div[@data-highcharts-chart='0']"))
        )
markers = chart.find_elements(By.XPATH, "//*[local-name()='g'][contains(@class,'highcharts-markers')]/*[local-name()='path']")
for m in markers:
    m.click()
    try:
        element = WebDriverWait(driver, 2).until(
            EC.presence_of_element_located((By.XPATH, "//*[local-name()='g'][contains(@class,'highcharts-label')]/*[local-name()='text']"))
        )
        tspans = element.find_elements(By.XPATH, "./*[local-name()='tspan']")
        if len(tspans) > 3: 
            print ("%s = %s" % (tspans[0].text, tspans[3].text))
    except TimeoutException:
        pass        

The output is as follows:

Q2-2015 = 2
Q3-2015 = 12
....
Q1-2022 = 5,076,978
Q2-2022 = 5,109,680
Q3-2022 = 5,122,480
Q4-2022 = 5,126,601
  • Related