Get value of an input box using Selenium (Python)
Asked Answered
R

3

62

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

AttributeError: 'WebElement' object has no attribute 'getText'
Ridotto answered 30/8, 2014 at 8:36 Comment(2)
Please attach sample html. Thanks.Sarcasm
does the input really contain text? or you want to get/store value 'name' of the input's attribute 'value'? Because get_text usually gives you this kind of text: <div>some_text</div>Unimproved
T
136

Use this to get the value of the input element:

input.get_attribute('value')
Tragopan answered 22/9, 2014 at 11:20 Comment(2)
For completeness: input.get_attribute('value').encode('utf-8')Phylogeny
while input.text does exist, it will not give you the value :(Pockmark
A
25

Note that there's an important difference between the value attribute and the value property.

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.

Basically, the value attribute sets the element's initial value, while the value property contains the current 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:

input.get_attribute('value')

If you want the value property, then you should use 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_property will always return the property.

Armelda answered 25/4, 2019 at 7:29 Comment(0)
R
0

Use this to get the value of the input element:

text =driver.find_element('xpath',"/html/body/div[2]/div/div[2]/div[2]/input")
value = text.get_dom_attribute("value")
print(value)
Radiopaque answered 15/3, 2023 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.