How to simulate pressing enter in HTML text input with Selenium ? - GeeksforGeeks

15 Dec, 2021
Selenium is an inbuilt module available in python that allows users to make automated suites and tests. We can build code or scripts to perform tasks automatically in a web browser using selenium. Selenium is used to test the software by automation. Also, programmers can create automated test cases for the software or app using the selenium.
By reading this tutorial, users will be able to simulate pressing enter in HTML text input with selenium. Also, we will write a simple code that can search text on the Wikipedia website automatically and perform automated login on the Geeksforgeeks website.
Prerequisite:
Python 3.7+
Selenium
web driver
Users should have installed python 3.7+ in their system to work with the selenium. To install selenium run the below command on the terminal.
pip install selenium
Download chrome webdriver: Next, users need to download webdriver according to which browser they want to run automated software. Chrome webdriver is one of the best webdriver. Users can download chrome webdriver from here. While downloading the chrome webdriver, make sure that the webdriver version is compatible with the browser version.
To simulate the pressing enter, users can add the below line in the python automation script code.
HTML_ELEMENT.send_keys(Keys.ENTER)
Search text using selenium on Wikipedia: In this part, we will cover that how users can open Wikipedia sites and search text automatically on Wikipedia or other websites using selenium. 
Approach:
Import webdriver from seleniumInitialize webdriver pathOpen any URLFind the search element using any method from belowInput text into the search fieldPress enter key to search input text
Import webdriver from selenium
Initialize webdriver path
Open any URL
Find the search element using any method from below
Input text into the search field
Press enter key to search input text
Example:
Python3
# Python program to search automatically# on wikipedia using selenium # Import webdriverfrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom time import sleep, strftime # Initialize webdriver objectchromedriver_path = '<Chrome webdriver path>'webdriver = webdriver.Chrome(executable_path=chromedriver_path) try:    # Opening wikipedia website    webdriver.get("https://en.wikipedia.org")         # Finding the search field by id    input = webdriver.find_element_by_id("searchInput")         # Sending input text to search field    input.send_keys("Python")         # Pressing enter to search input text    input.send_keys(Keys.ENTER)    sleep(10) finally:         # Closing the webdriver    webdriver.close()
Note: Don’t forget to set the chrome web driver’s path.
Output:
Log in automatically to GeeksForGeeks using the selenium module: In this part, we will cover that how users can log in to Geeksforgeeks using the selenium bot. 
Approach:
Import webdriver from seleniumInitialize webdriver pathOpen Geeksforgeeks URLFind and press enter on the sign-in buttonFind the username and password element on Geeksforgeeks websiteSet username and password into the input fieldFind the login buttonPressing enter or click on the login button
Import webdriver from selenium
Initialize webdriver path
Open Geeksforgeeks URL
Find and press enter on the sign-in button
Find the username and password element on Geeksforgeeks website
Set username and password into the input field
Find the login button
Pressing enter or click on the login button
Example:
Python3
# Python program to login to the Geeksforgeeks# using selenium from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom time import sleep, strftime chromedriver_path = '<chrome web driver path>'webdriver = webdriver.Chrome(executable_path=chromedriver_path) try:    # Opening the geeksforgeeks website    webdriver.get('https://www.geeksforgeeks.org/')         # Clicking on the sign in button    signIn = webdriver.find_element_by_css_selector('#userProfileId > a')    signIn.click()    sleep(4)         # Finding the username input field and sending the username    username = webdriver.find_element_by_css_selector('#luser')    username.send_keys('<Geeksforgeeks Username>')         # Finding the password input field and sending password    password = webdriver.find_element_by_css_selector('#password')    password.send_keys('<Geeksforgeeks password>')      # Pressing enter on the signin button    button_login = webdriver.find_element_by_css_selector(        '#Login > button')    button_login.click()    sleep(6)     finally:    webdriver.close()
Note: Don’t forget to set web driver’s path, Geeksforgeeks username, and password.
Output:
gabaa406
Picked
Python-selenium
selenium
Python
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?