4

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.

2 Answers 2

3

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'))
Sign up to request clarification or add additional context in comments.

2 Comments

No, printing those node only give me sthg 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}")>, ...]
do you use the loop for node in nodes: ?
2

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") 

2 Comments

thx a lot, it works. only have to change this line: paths = driver.find_elements_by_tag_name("path") thx everyone's effort
That will find you all the paths whether or not they are children of g elements.

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.