How to send keyboard input to a textbox on a webpage using Python Selenium webdriver?

SeleniumAutomation TestingTesting Tools

We can send keyboard input to a textbox on a webpage in Selenium webdriver in Python using the method send_keys. The text to be entered is passed as a parameter to that method.

To perform keyboard actions, we can also use the send_keys method and then pass the class Keys.<key to be pressed> as a parameter to that method. To use the Keys class, we have to add from selenium.webdriver.common.keys import Keys statement to the code.

Syntax

i = driver.find_element_by_name("txt")
i.send_keys("Selenium")
i.send_keys(Keys.RETURN)

Let us try to send keyboard input to a textbox on a page −

Example

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
#set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.implicitly_wait(0.5)
#launch URL
driver.get("https://www.google.com/")
#identify text box
l = driver.find_element_by_class_name("gLFyf")
#send input
l.send_keys("Selenium")
#send keyboard input
l.send_keys(Keys.RETURN)

Output

raja
Published on 06-Apr-2021 08:29:46
Advertisements