Selenium Syntax
Selenium Syntax
com/in/yogesh-p-346440149
Prerequisites
1. Install Java: Make sure you have Java installed
on your system.
2. Set up an Integrated Development Environment
(IDE) like Eclipse or IntelliJ IDEA.
3. Download Selenium WebDriver for Java and add
it to your project. You can get it from the
Selenium official website
PAGE 4
Robot
The Robot class is used to simulate keyboard and mouse
actions at the system level.
Robot robot = new Robot();
// Simulate key press and release
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
DropDown
This code snippet demonstrates working with
dropdown (select) elements using the Select class.
Select dropdown = new Select(driver.findElement(By.id("dropdown_id")));
dropdown.selectByVisibleText("Option 1"); // Select by visible text
dropdown.selectByValue("value1"); // Select by value
dropdown.selectByIndex(0); // Select by index
List<WebElement> options = dropdown.getOptions(); // Get all options
WebElement selectedOption = dropdown.getFirstSelectedOption(); // Get
selected option
boolean isMultiple = dropdown.isMu // Check if multiple selection
dropdown.deselectAll(); // Deselect all options in a multiple
select
dropdown.deselectByVisibleText("Option 1"); // Deselect by visible text
dropdown.deselectByValue("value1"); // Deselect by value
dropdown.deselectByIndex(0); // Deselect by index
Alert
This code snippet deals with handling alerts in
Selenium.
Alert alert = driver.switchTo().alert();
JavascriptExecutor
This code snippet demonstrates how to use
JavascriptExecutor to perform actions on web elements.
JavascriptExecutor js = (JavascriptExecutor) driver;
// Click on an element
WebElement element = driver.findElement(By.id("element_id"));
js.executeScript("arguments[0].click();", element);
// Scroll to an element
WebElement element = driver.findElement(By.id("element_id"));
js.executeScript("arguments[0].scrollIntoView(true);", element);
TakesScreenshot
This code snippet captures a screenshot of a web page
using Selenium WebDriver.
DestFile);