Day 14: Selenium Functions Part 2
Shree Krishna Priya J
Mom | AI this, AI that | Created Test Case Generator using OpenAI - 80% Accuracy and 50% time efficiency | Entwinning AI and Automation at FEFundinfo | ISTQB AI Testing Certified |
Actions
The Actions class in Selenium provides a way to simulate complex user interactions, such as mouse movements, keypresses, and context menu interactions. Here's an example:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class ActionsExample {
??public static void main(String[] args) {
????// Set the path to the ChromeDriver executable
????System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
????// Create a new instance of ChromeDriver
????WebDriver driver = new ChromeDriver();
????// Navigate to a webpage
????driver.get("https://www.example.com");
????// Find an element to perform actions on
????WebElement element = driver.findElement(By.id("myElement"));
????// Create an instance of Actions class
????Actions actions = new Actions(driver);
????// Perform various actions
????actions.moveToElement(element).build().perform(); // Move mouse to element
????actions.click(element).build().perform(); // Click on element
????actions.sendKeys(element, "Hello, world!").build().perform(); // Type text into element
????// Close the browser
????driver.quit();
??}
}
In this example, we create an instance of the Actions class and perform various actions on a web element using methods like moveToElement(), click(), and sendKeys(). The build().perform() method is used to execute the actions.
Select:
The Select class in Selenium is used for interacting with dropdowns and multi-select elements. It provides methods to select options, deselect options, and retrieve selected options. Here's an example:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
领英推荐
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class SelectExample {
??public static void main(String[] args) {
????// Set the path to the ChromeDriver executable
????System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
????// Create a new instance of ChromeDriver
????WebDriver driver = new ChromeDriver();
????// Navigate to a webpage
????driver.get("https://www.example.com");
????// Find the dropdown element
????WebElement dropdown = driver.findElement(By.id("myDropdown"));
????// Create an instance of Select class
????Select select = new Select(dropdown);
????// Select options
????select.selectByVisibleText("Option 1");
????select.selectByValue("option2");
????// Deselect option
????select.deselectByVisibleText("Option 1");
????// Get all selected options
????List<WebElement> selectedOptions = select.getAllSelectedOptions();
????for (WebElement option : selectedOptions) {
??????System.out.println("Selected option: " + option.getText());
????}
????// Close the browser
????driver.quit();
??}
}
In this example, we locate a dropdown element using the findElement() method, create an instance of the Select class, and perform operations like selecting options using selectByVisibleText() and selectByValue(), deselecting options using deselectByVisibleText(), and retrieving all selected options using getAllSelectedOptions().
These coding examples provide a detailed understanding of Selenium WebElement, WebDriver, By, Actions, and Select, along with practical usage. You can use these examples as a starting point to experiment and explore further in your Selenium automation projects.
To practice more on various elements and functions in a webpage, use this post https://www.dhirubhai.net/posts/shree-krishna-priya-j-9737477a_practice-page-activity-6812408460479627264-IAbu?utm_source=share&utm_medium=member_desktop
To find the xpath or css, learn the basics but use SelectorsHub to save a lot of time in your automation journey.