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.
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.
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.
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.
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.
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.
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.
21/09/2024 | UI Automation
18/09/2024 | UI Automation
21/09/2024 | UI Automation
21/09/2024 | UI Automation
18/09/2024 | UI Automation
21/09/2024 | UI Automation
21/09/2024 | UI Automation
21/09/2024 | UI Automation
21/09/2024 | UI Automation