Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Webscraping
  • Beautiful Soup
  • Request
  • Scrapy
  • open cv
  • Data analysis
  • Machine learning
  • NLP
  • Deep learning
  • Data Science
  • Interview question
  • ML math
  • ML Projects
  • ML interview
  • DL interview
Open In App
Next Article:
How to simulate pressing enter in HTML text input with Selenium ?
Next article icon

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

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  1. Import webdriver from selenium
  2. Initialize webdriver path
  3. Open any URL
  4. Find the search element using any method from below
  5. Input text into the search field
  6. 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:

  1. Import webdriver from selenium
  2. Initialize webdriver path
  3. Open Geeksforgeeks URL
  4. Find and press enter on the sign-in button
  5. Find the username and password element on Geeksforgeeks website
  6. Set username and password into the input field
  7. Find the login button
  8. 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:


Next Article
How to simulate pressing enter in HTML text input with Selenium ?

S

shubhamvora05
Improve
Article Tags :
  • HTML
  • Python-selenium

Similar Reads

    How to pre-select an input element when the page loads in HTML5?
    In this article, we are going to pre-select an input element when the page loads i.e. the user doesn't have to click the first text box to start writing on it. When the page is loaded the input is to be pre-selected and the user can start typing directly. This can be done by using the autofocus attr
    1 min read
    How to wait until an element is present in Selenium?
    Selenium is a powerful tool for automating web browsers, often used for testing web applications. However, web pages that load content dynamically can present challenges, especially when you need to interact with elements that may not be immediately available. In such cases, waiting for an element t
    2 min read
    How to clear all content in html text box using Selenium?
    Selenium is one of the most popular tools out there when it comes to automating web applications. Selenium is widely used for automating user interactions like filling out the forms, clicking on buttons, or navigating to a page. Selenium provides a programmable interface where users can write automa
    5 min read
    Working with Input box/Test Box in Selenium with Python
    Selenium is an effective device for controlling an internet browser through the program. It is purposeful for all browsers, works on all fundamental OS and its scripts are written in numerous languages i.e Python, Java, C#, etc, we can be running with Python.Working with Input box/Test Box, let us h
    1 min read
    How to input text in the text box without calling the sendKeys() using Selenium java?
    When automating web applications using Selenium WebDriver, the standard approach to input text into a text box is by using the sendKeys() method. However, there are scenarios where you might want to input text without calling sendKeys(). This can be achieved using JavaScriptExecutor in Selenium. Jav
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences