Derekyy
Derekyy

Reputation: 1038

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: http://www.totalcorner.com/match/live_stats/57565755

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 updated solution:

thanks everyone's effort. After Robert Longson's updated answer, I think the following is the better solution:

nodes = driver.find_elements_by_xpath("//div[@id='all_container']/*[@id='highcharts-0']/*[name()='svg']/*[name()='g']/*[name()='path']")
for node in nodes:
    print(node.get_attribute("d"))

Since I cannot differentiate paths if using driver.find_elements_by_tag_name, I think the answer above is better.

Upvotes: 4

Views: 3726

Answers (2)

Ulf Gjerdingen
Ulf Gjerdingen

Reputation: 1430

You are getting a list, so try:

for node in nodes:
    print(node.text)

if a you are looking for a value of an attribute then use the following (href here as an example):

print(node.get_attribute('href'))

Upvotes: 3

Robert Longson
Robert Longson

Reputation: 124059

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.

E.g.

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

Upvotes: 2

Related Questions