Get value of an input box using Selenium (Python)

PythonSeleniumSelenium Webdriver

Python Problem Overview


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'

Python Solutions


Solution 1 - Python

Use this to get the value of the input element:

input.get_attribute('value')

Solution 2 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKhalilView Question on Stackoverflow
Solution 1 - PythonSaturiView Answer on Stackoverflow
Solution 2 - PythonPikamander2View Answer on Stackoverflow