Selenium Webdriver - Handling Edit Boxes



Selenium can be used to input text to an edit box. An edit box is represented by the input tag and its type attribute should have the value as text. It can be identified with any of the locators like - id, class, name, css, xpath and tagname.

To input a value into an edit box, we have to use the method send_keys.

Let us see the html code of a webelement −

Handling Edit Boxes

The edit box highlighted in the above image has a tagname - input. Let us try to input some text into this edit box after identifying it.

Code Implementation

The code implementation for handling edit box is as follows −

from selenium import webdriver
#set chromedriver.exe path
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#url launch
driver.get("https://www.tutorialspoint.com/index.htm")
#identify edit box with tagname
l = driver.find_element_by_tag_name('input')
#input text
l.send_keys('Selenium Python')
#obtain value entered
v = l.get_attribute('value')
print('Value entered: ' + v)
#driver close
driver.close()

Output

Handling Edit Boxes Output

The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. Also, the value entered within the edit box (obtained from the get_attribute method) - Selenium Python gets printed in the console.

Advertisements