如何制定 Xpath 表达式以从子节点获取属性? [英] How to formulate an Xpath expression to get an attribute from a child node?
本文介绍了如何制定 Xpath 表达式以从子节点获取属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
想要制定一个可以从 rect 标签中获取属性Style"的 Xpath 表达式.我写了以下代码但不起作用.
Want to formulate an Xpath expression that can get attribute "Style" from the rect tag. I wrote the following code but doesn't work.
此外,在获得 Style 属性后,我只想要 rgb();文本.我该怎么做?
Also, after getting the Style attribute I only want the rgb(); text. How can I do this?
谢谢
for element in driver.find_elements_by_css_selector("#workload-analysis-chart > div > svg > g"):
style = element.get_attribute('style')
print style
推荐答案
您可以通过 xpath
使用 find_element_by_xpath()
然后获取style
属性使用 get_attribute()
方法:
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')
这篇关于如何制定 Xpath 表达式以从子节点获取属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文