logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Selenium Interacting with Web Elements

author
Generated by
Hitendra Singhal

21/09/2024

Selenium

Sign in to read full article

Selenium is an open-source automation tool that allows developers and testers to automate browser activities seamlessly. One of the core functionalities of Selenium is its ability to interact with web elements. In this blog, we will discuss three primary actions you can perform on web elements: Click, Send Keys, and Select.

1. Click

The Click action is one of the most frequently used interactions in Selenium. It allows you to simulate a mouse click on web elements, like buttons or links. This is essential for navigating through a web application or triggering particular events.

Example of Click Action:

from selenium import webdriver from selenium.webdriver.common.by import By # Initiate the browser driver = webdriver.Chrome() # Open a webpage driver.get('https://example.com') # Locate the button by its ID and click it button = driver.find_element(By.ID, "submit-button") button.click() # Close the browser driver.quit()

In this example, we initiate a Chrome browser, navigate to a sample webpage, locate the button with ID "submit-button," and perform the click action.

2. Send Keys

The Send Keys action simulates typing into web elements such as text fields or text areas. This is necessary when inputting data, such as filling out forms or search boxes.

Example of Send Keys Action:

from selenium import webdriver from selenium.webdriver.common.by import By # Initiate the browser driver = webdriver.Chrome() # Open a webpage driver.get('https://example.com/login') # Locate the username field and send keys username_field = driver.find_element(By.NAME, "username") username_field.send_keys("myUsername") # Locate the password field and send keys password_field = driver.find_element(By.NAME, "password") password_field.send_keys("myPassword") # Close the browser driver.quit()

In this example, we navigate to a login page and locate the username and password fields. Using the Send Keys method, we input our credentials for authentication.

3. Select

When working with dropdown menus, you will need to use the Select class from Selenium. This allows you to choose options from <select> HTML elements efficiently.

Example of Select Action:

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select # Initiate the browser driver = webdriver.Chrome() # Open a dropdown page driver.get('https://example.com/form') # Locate the dropdown element dropdown = Select(driver.find_element(By.ID, "options")) # Select by visible text, index or value dropdown.select_by_visible_text("Option 1") # dropdown.select_by_index(0) # Select the first item # dropdown.select_by_value("value1") # Select by the option value # Close the browser driver.quit()

In this code snippet, we navigate to a web page that features a form with a dropdown list. We locate the dropdown by its ID and use the Select class to choose an option by its visible text.

By mastering these three fundamental actions—Click, Send Keys, and Select—you can significantly enhance your web automation scripts in Selenium. Each of these methods opens the door to mimic real user behavior within your automated tests while ensuring a dynamic interaction with your application's web elements.

Popular Tags

SeleniumWeb AutomationUI Testing

Share now!

Like & Bookmark!

Related Collections

  • Mastering Selenium WebDriver: Automation Testing Essentials

    21/09/2024 | UI Automation

  • Mastering UI Automation: Practical Guide

    18/09/2024 | UI Automation

Related Articles

  • Setting Up the Automation Environment

    18/09/2024 | UI Automation

  • Selenium Data-Driven Testing with Excel and CSV

    21/09/2024 | UI Automation

  • Mastering Selenium with Page Object Model Design Pattern

    21/09/2024 | UI Automation

  • Running Selenium Tests in Headless Mode

    21/09/2024 | UI Automation

  • Understanding Selenium WebDriver Architecture and Components

    21/09/2024 | UI Automation

  • Selenium Synchronization

    21/09/2024 | UI Automation

  • Handling Dynamic Web Elements in Selenium

    21/09/2024 | UI Automation

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design