使用 Selenium (Python) 获取输入框的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25580569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get value of an input box using Selenium (Python)
提问by Khalil
I am trying to extract the text in an input box,
我正在尝试提取输入框中的文本,
<input type="text" name="inputbox" value="name" class="box">
I started with
我开始了
input = driver.find_element_by_name("inputbox")
I tried input.getText() but I got
我试过 input.getText() 但我得到了
AttributeError: 'WebElement' object has no attribute 'getText'
采纳答案by Saturi
Use this to get the value of the input element:
使用它来获取输入元素的值:
input.get_attribute('value')
回答by Pikamander2
Note that there's an important difference between the value attribute and the value property.
请注意, value 属性和 value 属性之间存在重要区别。
The simplified explanation is that the value attribute is what's found in the HTML tag and the value property is what you see on the page.
简化的解释是 value 属性是在 HTML 标记中找到的内容,而 value 属性是您在页面上看到的内容。
Basically, the value attribute sets the element's initial value, while the value property contains the current value.
基本上, value 属性设置元素的初始值,而 value 属性包含当前值。
You can read more about that hereand see an example of the difference here.
If you want the valueattribute, then you should use get_attribute:
如果你想要value属性,那么你应该使用 get_attribute:
input.get_attribute('value')
If you want the valueproperty, then you should use get_property
如果你想要这个value属性,那么你应该使用 get_property
input.get_property("value")
Though, according to the docs, get_attributeactually returns the property rather than the attribute, unless the property doesn't exist. get_propertywill always return the property.
尽管如此,根据文档,get_attribute实际上返回的是属性而不是属性,除非该属性不存在。get_property将始终返回该属性。

