简体   繁体   English

使用 Selenium (Python) 获取输入框的值

[英]Get value of an input box using Selenium (Python)

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'

使用它来获取输入元素的值:

input.get_attribute('value')

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 here and see an example of the difference here .你可以阅读更多有关在这里,看到了差距的例子在这里


If you want the value attribute , then you should use get_attribute:如果你想要value属性,那么你应该使用 get_attribute:

input.get_attribute('value')

If you want the value property , then you should use get_property如果你想要value属性,那么你应该使用 get_property

input.get_property("value")

Though, according to the docs, get_attribute actually returns the property rather than the attribute, unless the property doesn't exist.尽管如此,根据文档, get_attribute实际上返回的是属性而不是属性,除非该属性不存在。 get_property will always return the property. get_property将始终返回该属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM