Python Selenium:使用 xpath 查找对象属性 [英] Python Selenium: Find object attributes using xpath

查看:47
本文介绍了Python Selenium:使用 xpath 查找对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 xpath 的新手,尝试使用 xpath 获取值"的值:

I am new to xpath, trying to get value of the "value" using xpath:

<input type="submit" value="  Search  " class="long searchButton" style="width:190px !important;">

虽然通过 "type="submit" 很容易找到元素,例如:

while it is easy to find element by "type="submit" like:

browser.find_elements_by_xpath("//*[@type='submit']")

我一直无法弄清楚如何获得我需要的值,因为:

I haven't been able to figure out how to get the values I need, as:

browser.find_elements_by_xpath("//*[@type='submit']/@value")

有点出乎意料的错误:

expression "//*[@type=\'submit\']/@value" is: [object Attr]. It should be an element

任何想法如何解决这个问题?

Any ideas how to solve this?

xpath 是正确的,但它返回"了一个 obj 属性,因为它不是一个元素,所以是不允许的.我找不到像 get_attr_by_xpath() 或类似方法.

The xpath is correct, but it "returns" an obj attribute and as it is not an element, it is not allowed. I can't find a method like get_attr_by_xpath(), or anything similar.

推荐答案

我最终使用了 get_attribute("value") 作为:

I finally used get_attribute("value") as:

for i in browser.find_elements_by_xpath("//*[@type='submit']"):
    print i.get_attribute("value")

这篇关于Python Selenium:使用 xpath 查找对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆