Question

enter image description here

Want to formulate an Xpath expression that can get attribute "Style" from the rect tag. I wrote the following code but doesn't work.

Also, after getting the Style attribute I only want the rgb(); text. How can I do this?

Thanks

   for element in driver.find_elements_by_css_selector("#workload-analysis-chart > div > svg > g"):
            style = element.get_attribute('style')
            print style
Was it helpful?

Solution

You can find the element by xpath using find_element_by_xpath() and then get the style attribute using get_attribute() method:

rect = driver.find_element_by_xpath('//div[@id="workload-analysis-chart"]/div/svg/g/g[@data-selected="true"]/rect')
print rect.get_attribute('style')

OTHER TIPS

The expression is not xpath, but a css selector.

Changing the expression as following will print the style attributes:

for rect in root.cssselect('#workload-analysis-chart > div > svg > g > g > rect'):
    style = rect.get('style')
    print style

try using lxml it have nice xpath feature

root = lxml.html.fromstring(pagesource)
#get rect with data-eid="10"
rect = root.xpath('//rect[@data-eid="10"]')
r = rect[0] #^returned list of rects
style = r.get("default-fill")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top