Derekyy Derekyy - 9 months ago 74
Python Question

Get path in svg using Selenium (Python)

I have the following code which find all g's in svg, yet how could I get those path elements inside g's and their path value?

I am testing with this website:

related code:

nodes = self.driver.find_elements_by_xpath("//div[@id='all_container']/*[@id='highcharts-0']/*[name()='svg']/*[name()='g']")

I have already tried this:

nodes = self.driver.find_elements_by_xpath("//div[@id='all_container']/*[@id='highcharts-0']/*[name()='svg']/*[name()='g']/*[name()='path']")

yet what I get is something like this:

[<selenium.webdriver.remote.webelement.WebElement (session="fb86fb35-d2fa-974a-af32-a15db1b7459d", element="{c1dad34f-764d-0249-9302-215dd9ae9cd8}")>, <selenium.webdriver.remote.webelement.WebElement (session="fb86fb35-d2fa-974a-af32-a15db1b7459d", element="{a53816f4-9952-ab49-87ac-5d79538a855d}")>, ...]

How could I use this to find the path value?
thanks a lot

My Solution:

So now my solution is as such: after calling


which ensures the svg graph is loaded, I find those paths by driver.find_elements_by_x (whatever would be alright)


find_elements_by_tag_name can be used to find path children.

Once you have those, get_attribute("d") will get you the path value.


for node in nodes:
    paths = node.find_elements_by_tag_name("path")
    for path in paths:
        value = path.get_attribute("d")