How to simulate pressing enter in HTML text input with Selenium ?
Last Updated :
25 Jul, 2024
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:
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 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:
Python
# Python program to search automatically
# on wikipedia using selenium
# Import webdriver
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime
# Initialize webdriver object
chromedriver_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 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:
Python
# Python program to login to the Geeksforgeeks
# using selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from 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:
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
HTML Tutorial HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
11 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read