How to enter values in an edit box in Selenium with python?

Selenium Web DriverAutomation TestingSoftware Testing

We can enter values in an edit box in Selenium with the help of the methods listed below −

  • Using the send_keys method.

    This method can send any text to an edit box or perform pressing keys with the help of Keys class.

  • Using the Javascript executor.

    Javascript Document Object Model can work with any of the elements on the page. Javascript works on the client side and performs actions on the web page. Selenium can execute a Javascript script with the help of execute_script() method. We can enter values on any edit box with the help of this method.

Example

Code Implementation with send_keys method.

from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke #actual browser
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://www.tutorialspoint.com/index.htm")
#to refresh the browser
driver.refresh()
# identifying the edit box and entering text with send_keys method
driver. find_element_by_css_selector("input[class='gsc-input']").
send_keys("Selenium")
#to close the browser
driver.close()

Code Implementation with Javascript executor.

from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke #actual browser
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://www.tutorialspoint.com/index.htm")
#to refresh the browser
driver.refresh()
# enter text with the Javascript executor
driver.execute_script(
"document.getElementsByName('search')[0].value = 'Selenium' ;")
#to close the browser
driver.close()
raja
Published on 29-Jul-2020 11:17:17
Advertisements