我是xpath的新手,试图使用xpath获取“值”的值:
<input type="submit" value=" Search " class="long searchButton" style="width:190px !important;">
虽然很容易通过"type="submit"
找到元素,如:
browser.find_elements_by_xpath("//*[@type='submit']")
我无法弄清楚如何获得我需要的值,如:
browser.find_elements_by_xpath("//*[@type='submit']/@value")
有点预期会出错:
expression "//*[@type=\'submit\']/@value" is: [object Attr]. It should be an element
任何想法如何解决这个问题?
编辑: xpath是正确的,但它“返回”一个obj属性,因为它不是一个元素,所以不允许。我找不到像get_attr_by_xpath()这样的方法,或类似的东西。
答案 0 :(得分:29)
我最后使用get_attribute("value")
作为:
for i in browser.find_elements_by_xpath("//*[@type='submit']"):
print i.get_attribute("value")
答案 1 :(得分:2)
就像这样
browser.find_elements_by_xpath("//*[@type='submit']/@value").text
<强>更新强>
使用您使用的功能,我们只能提取元素而不是其属性。要获取其属性,表达式应该是这样的
browser.find_elements_by_xpath("//*[@type='submit']").get_attribute("value")
答案 2 :(得分:1)
I certainly know this was posted over 2 years ago, but I would like to add a little to it which was left out. selenium provides built-in words that could be use to pull the text that is in between the tags or the actual attribute values inside the tags.
${XPATH} xpath=(//td[@title='mytitle']/../td)[2]
Here is a quick example:
${THE_NAME}= Get Text ${XPATH}