Selenium Guide

Setup — Java & Python

// Java (Maven dependency) // <dependency>org.seleniumhq.selenium:selenium-java:4.x</dependency> import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; ChromeOptions options = new ChromeOptions(); options.addArguments("--headless=new", "--no-sandbox", "--disable-dev-shm-usage"); WebDriver driver = new ChromeDriver(options); driver.get("https://example.com"); System.out.println(driver.getTitle()); driver.quit(); --- # Python (pip install selenium) from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service options = Options() options.add_argument("--headless=new") options.add_argument("--no-sandbox") driver = webdriver.Chrome(options=options) driver.get("https://example.com") print(driver.title) driver.quit()

Locators

// Java import org.openqa.selenium.By; driver.findElement(By.id("email")); driver.findElement(By.name("password")); driver.findElement(By.className("btn-primary")); driver.findElement(By.tagName("input")); driver.findElement(By.linkText("Sign In")); driver.findElement(By.partialLinkText("Sign")); driver.findElement(By.cssSelector("input[type='email']")); driver.findElement(By.xpath("//button[@type='submit']")); // Selenium 4 — relative locators import static org.openqa.selenium.support.locators.RelativeLocator.*; driver.findElement(with(By.tagName("label")).above(By.id("email"))); --- # Python from selenium.webdriver.common.by import By driver.find_element(By.ID, "email") driver.find_element(By.CSS_SELECTOR, "input[type='email']") driver.find_element(By.XPATH, "//button[@type='submit']") driver.find_elements(By.CLASS_NAME, "list-item") # multiple

Explicit Waits

// Java import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; import java.time.Duration; WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); // Wait for element to be visible WebElement el = wait.until( ExpectedConditions.visibilityOfElementLocated(By.id("result")) ); // Wait for URL wait.until(ExpectedConditions.urlContains("/dashboard")); // Wait for text wait.until(ExpectedConditions.textToBePresentInElementLocated( By.id("msg"), "Success")); --- # Python from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, 10) el = wait.until(EC.visibility_of_element_located((By.ID, "result"))) wait.until(EC.url_contains("/dashboard")) wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#submit")))

Page Object Model

// Java — LoginPage.java import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; public class LoginPage { private final WebDriver driver; @FindBy(id = "email") private WebElement emailField; @FindBy(id = "password") private WebElement passwordField; @FindBy(css = "button[type='submit']") private WebElement submitButton; public LoginPage(WebDriver driver) { this.driver = driver; PageFactory.initElements(driver, this); } public DashboardPage login(String email, String password) { emailField.sendKeys(email); passwordField.sendKeys(password); submitButton.click(); return new DashboardPage(driver); } } // Usage in test LoginPage loginPage = new LoginPage(driver); DashboardPage dashboard = loginPage.login("[email protected]", "pass"); assertTrue(dashboard.isLoaded());

Actions — Forms & Keyboard

// Java — Actions class import org.openqa.selenium.interactions.Actions; Actions actions = new Actions(driver); actions.moveToElement(element).click().perform(); actions.sendKeys(element, "text").perform(); actions.doubleClick(element).perform(); actions.contextClick(element).perform(); // right-click // Keyboard shortcuts actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform(); // Select dropdown import org.openqa.selenium.support.ui.Select; Select select = new Select(driver.findElement(By.id("country"))); select.selectByVisibleText("United States"); select.selectByValue("us"); select.selectByIndex(1); --- # Python from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select actions = ActionChains(driver) actions.move_to_element(element).click().perform() actions.key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL).perform() select = Select(driver.find_element(By.ID, "country")) select.select_by_visible_text("China")

Grid Setup

# Selenium Grid 4 — start hub and node # Hub (router) java -jar selenium-server-4.x.jar hub # Node java -jar selenium-server-4.x.jar node \ --hub http://localhost:4444 # Docker Compose # services: # chrome: # image: selenium/node-chrome:4 # firefox: # image: selenium/node-firefox:4 # hub: # image: selenium/hub:4 // Java — connect to Grid import org.openqa.selenium.remote.RemoteWebDriver; import java.net.URL; ChromeOptions options = new ChromeOptions(); WebDriver driver = new RemoteWebDriver( new URL("http://localhost:4444"), options );