Get value of an input box using Selenium (Python)


We can get the value of an input box with Selenium webdriver. The get_attribute() method is capable of obtaining the value we have entered in an input box. To get the value, we have to pass value as a parameter to the method.

First of all, we have to identify the input box with the help of any of the locators like id, class, name, css or xpath. Then we have to type some values inside it with the send_keys() method.

Let us consider the below input box where we shall enter some texts - Selenium Python and then fetch the value with get_attribute().

Example

from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
driver.implicitly_wait(0.5)
driver.get("https://www.google.com/")
#identify element
l= driver.find_element_by_name("q")
l.send_keys("q")
#get_attribute() to get value of input box
print("Value of input box: " + l.get_attribute('value'))
driver.close()

Output

Updated on: 18-Sep-2020

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements