Selenium 4 Command Reference for SDETs
qabash.comAmbassador
Feb 8, 2026

Updated Feb 2026: Selenium 4.18+, Chrome 122+, WebDriverManager 5.6+
Selenium remains essential for legacy framework maintenance and specific browser requirements despite Playwright’s rise.
WebDriver Setup (Auto-Managed)
// Chrome (Recommended)
WebDriver driver = new ChromeDriver();
// Firefox
WebDriver driver = new FirefoxDriver();
// Edge
WebDriver driver = new EdgeDriver();
// Headless Chrome (CI/CD)
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless", "--no-sandbox", "--disable-dev-shm-usage");
WebDriver driver = new ChromeDriver(options);
Browser Navigation
driver.get("https://example.com"); // Direct URL
driver.navigate().to("https://example.com"); // Navigate object
driver.navigate().back(); // Browser back
driver.navigate().forward(); // Browser forward
driver.navigate().refresh(); // Page refresh
String title = driver.getTitle(); // Page title
String url = driver.getCurrentUrl(); // Current URL
String source = driver.getPageSource(); // HTML source
Browser Operations
driver.manage().window().maximize(); // Maximize
driver.manage().window().minimize(); // Minimize
driver.manage().window().fullscreen(); // Fullscreen
driver.quit(); // Close all windows + end session
driver.close(); // Close current window only
Element Locators (Priority Order)
| Priority | Strategy | Syntax | Speed |
|---|---|---|---|
| 1 | ID | By.id("submit-btn") | Fastest |
| 2 | CSS | By.cssSelector("#submit.btn-primary") | Fast |
| 3 | Name | By.name("username") | Good |
| 4 | XPath | By.xpath("//button[@data-test='submit']") | Slowest |
// Multiple elements
List<WebElement> elements = driver.findElements(By.cssSelector(".item"));
// Absence check
Assert.assertTrue(driver.findElements(By.id("nonexistent")).isEmpty());
Waits (Essential!)
// NEVER USE Thread.sleep() - Anti-pattern!
// Implicit Wait (Global)
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// Explicit Wait (Recommended)
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.id("submit")));
// Fluent Wait (Advanced)
Wait<WebDriver> fluentWait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofSeconds(2))
.ignoring(NoSuchElementException.class);
Web Elements
WebElement element = driver.findElement(By.id("username"));
element.sendKeys("testuser"); // Type text
element.click(); // Click
element.clear(); // Clear input
String text = element.getText(); // Visible text
String attr = element.getAttribute("value"); // Attribute value
boolean displayed = element.isDisplayed(); // Visible?
boolean enabled = element.isEnabled(); // Clickable?
boolean selected = element.isSelected(); // Checkbox/radio
Dropdowns (Select Class)
WebElement dropdown = driver.findElement(By.id("country"));
Select select = new Select(dropdown);
select.selectByVisibleText("India"); // By text
select.selectByValue("IN"); // By value
select.selectByIndex(1); // By position
// Multi-select
select.deselectByValue("IN");
select.deselectAll();
Alerts & Popups
Alert alert = driver.switchTo().alert();
alert.accept(); // OK
alert.dismiss(); // Cancel
String text = alert.getText(); // Message
alert.sendKeys("input"); // Prompt only
Windows & Frames
// Windows/Tabs
String mainWindow = driver.getWindowHandle();
for(String window : driver.getWindowHandles()) {
driver.switchTo().window(window);
if(!window.equals(mainWindow)) driver.close();
}
driver.switchTo().window(mainWindow);
// Frames/iFrames
driver.switchTo().frame("frameId"); // By ID/Name
driver.switchTo().frame(0); // By index
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
driver.switchTo().defaultContent(); // Back to main
Screenshots (Debug Gold!)
// Full page
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("screenshot.png"));
// Element screenshot
File elementShot = element.getScreenshotAs(OutputType.FILE);
Actions Class (Advanced Interactions)
Actions actions = new Actions(driver);
actions.moveToElement(element).perform(); // Hover
actions.contextClick(element).perform(); // Right-click
actions.doubleClick(element).perform(); // Double-click
actions.dragAndDrop(source, target).perform(); // Drag & drop
JavaScript Executor
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, 500);"); // Scroll
js.executeScript("arguments[0].click();", element); // Force click
String title = js.executeScript("return document.title;").toString();
Headless Mode (CI/CD)
ChromeOptions options = new ChromeOptions();
options.addArguments(
"--headless=new", // New headless (2023+)
"--window-size=1920,1080",
"--disable-gpu",
"--no-sandbox"
);
WebDriver driver = new ChromeDriver(options);
TestNG Assertions
Assert.assertEquals(actual, expected, "Message"); // Equality
Assert.assertTrue(condition, "Must be true"); // Condition
Assert.assertFalse(condition, "Must be false"); // Negative
Assert.assertNotNull(element, "Element required");
Common Exceptions & Fixes
| Exception | Cause | Fix |
|---|---|---|
| NoSuchElement | Locator wrong | Verify selector in DevTools |
| StaleElement | DOM refresh | Re-locate after page change |
| Timeout | Element slow | Explicit Wait > Implicit |
| ElementNotInteractable | Hidden/Disabled | js.click() or scroll into view |
Pro Tips for SDETs
- Always use Explicit Waits over Thread.sleep()
- Prefer CSS selectors over XPath (10x faster)
- Use
WebDriverManager– no manual driver downloads:javaWebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); - Page Object Model – never use raw locators in tests
- Screenshot on failure – TestNG
@AfterMethodlistener
Download Printable PDF: [Link to cheatsheet PDF]
Selenium WebDriver 2026 Cheat Sheet – Exhaustive
1. WebDriver Setup & Management
| Action | Java Code | Notes |
|---|---|---|
| ChromeDriver | WebDriver driver = new ChromeDriver(); | Auto-managed via Selenium Manager |
| FirefoxDriver | WebDriver driver = new FirefoxDriver(); | GeckoDriver auto-download |
| EdgeDriver | WebDriver driver = new EdgeDriver(); | Edge WebDriver |
| Headless Chrome | ChromeOptions ops = new ChromeOptions(); ops.addArguments("--headless=new"); | CI/CD essential |
| Window Size | driver.manage().window().setSize(new Dimension(1920,1080)); | Responsive testing |
| Maximize | driver.manage().window().maximize(); | Default for desktop |
| Minimize | driver.manage().window().minimize(); | macOS only |
| Fullscreen | driver.manage().window().fullscreen(); | Kiosk mode |
| Quit Session | driver.quit(); | Always use – closes all windows |
| Close Window | driver.close(); | Current window only |
2. Browser Navigation & Info
| Action | Java Code | Returns |
|---|---|---|
| Navigate GET | driver.get("https://example.com"); | void |
| Navigate TO | driver.navigate().to("https://example.com"); | void |
| Back | driver.navigate().back(); | void |
| Forward | driver.navigate().forward(); | void |
| Refresh | driver.navigate().refresh(); | void |
| Current URL | driver.getCurrentUrl(); | String |
| Page Title | driver.getTitle(); | String |
| Page Source | driver.getPageSource(); | String (HTML) |
3. Locators (Priority: ID > CSS > XPath)
| Priority | Strategy | Syntax | Example |
|---|---|---|---|
| 1 | ID | By.id("submit") | driver.findElement(By.id("login-btn")) |
| 2 | CSS Selector | By.cssSelector("#submit.btn") | By.cssSelector("input[type='email']") |
| 3 | Name | By.name("username") | By.name("password") |
| 4 | Class Name | By.className("btn-primary") | Multiple classes: .class1.class2 |
| 5 | Tag Name | By.tagName("input") | Rarely used alone |
| 6 | Link Text | By.linkText("Click Here") | Exact match |
| 7 | Partial Link | By.partialLinkText("Click") | Contains text |
| 8 | XPath | By.xpath("//button[@data-test='submit']") | Last resort |
Find Multiple Elements:
List<WebElement> elements = driver.findElements(By.cssSelector(".item"));
// Check absence: elements.isEmpty()
4. Waits (Critical!)
| Wait Type | Code | When to Use |
|---|---|---|
| Implicit | driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); | Global timeout |
| Explicit | new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(locator)); | RECOMMENDED |
| Fluent | new FluentWait(driver).withTimeout(Duration.ofSeconds(30)).ignoring(NoSuchElementException.class); | Custom polling |
| Page Load | driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30)); | Slow pages |
| Script | driver.manage().timeouts().setScriptTimeout(Duration.ofSeconds(10)); | JS execution |
Common ExpectedConditions:
visibilityOfElementLocated(locator)
elementToBeClickable(locator)
presenceOfElementLocated(locator)
textToBePresentInElement(element, "text")
numberOfWindowsToBe(2)
5. WebElement Operations
| Action | Code | Returns |
|---|---|---|
| Send Keys | element.sendKeys("test@example.com"); | void |
| Click | element.click(); | void |
| Clear | element.clear(); | void |
| Get Text | element.getText(); | String |
| Get Attribute | element.getAttribute("value"); | String |
| Is Displayed | element.isDisplayed(); | boolean |
| Is Enabled | element.isEnabled(); | boolean |
| Is Selected | element.isSelected(); | boolean (checkbox/radio) |
6. Dropdown Operations (Select Class)
| Action | Code | Notes |
|---|---|---|
| Initialize | Select select = new Select(dropdownElement); | <select> only |
| Select by Text | select.selectByVisibleText("India"); | User-visible text |
| Select by Value | select.selectByValue("IN"); | value attribute |
| Select by Index | select.selectByIndex(0); | 0-based position |
| Get Selected | select.getFirstSelectedOption().getText(); | Single select |
| Deselect | select.deselectByValue("IN"); | Multi-select only |
| Deselect All | select.deselectAll(); | Multi-select |
7. Alerts & Popups
| Action | Code | Notes |
|---|---|---|
| Switch to Alert | driver.switchTo().alert(); | JS confirm/prompt |
| Accept (OK) | alert.accept(); | void |
| Dismiss (Cancel) | alert.dismiss(); | void |
| Get Text | alert.getText(); | String |
| Send Keys | alert.sendKeys("input"); | Prompts only |
8. Windows, Tabs & Frames
Windows/Tabs:
| Action | Code |
|---|---|
| Get Current | driver.getWindowHandle(); |
| Get All | driver.getWindowHandles(); |
| Switch Window | driver.switchTo().window(handle); |
| Close & Switch Back | driver.close(); driver.switchTo().window(mainWindow); |
Frames/iFrames:
| Action | Code |
|---|---|
| Frame by Index | driver.switchTo().frame(0); |
| Frame by Name/ID | driver.switchTo().frame("frameName"); |
| Frame by Element | driver.switchTo().frame(element); |
| Back to Default | driver.switchTo().defaultContent(); |
9. Screenshots
| Type | Code |
|---|---|
| Full Page | ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); |
| Element | element.getScreenshotAs(OutputType.FILE); |
| Base64 | ((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64); |
// Save to file
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("screenshot.png"));
10. Actions Class (Advanced)
| Action | Code |
|---|---|
| Hover | new Actions(driver).moveToElement(element).perform(); |
| Right Click | new Actions(driver).contextClick(element).perform(); |
| Double Click | new Actions(driver).doubleClick(element).perform(); |
| Drag & Drop | new Actions(driver).dragAndDrop(source, target).perform(); |
| Click & Hold | new Actions(driver).clickAndHold(element).release().perform(); |
| Scroll to Element | new Actions(driver).scrollToElement(element).perform(); |
11. JavaScript Executor
| Action | Code |
|---|---|
| Scroll Down | (JavascriptExecutor)driver.executeScript("window.scrollBy(0,500);"); |
| Force Click | (JavascriptExecutor)driver.executeScript("arguments[0].click();", element); |
| Get Title | driver.executeScript("return document.title;").toString(); |
| Set Attribute | driver.executeScript("arguments[0].value='test';", element); |
| Check Visibility | driver.executeScript("return arguments[0].checkVisibility();", element); |
12. Cookies Management
| Action | Code |
|---|---|
| Get All | driver.manage().getCookies(); |
| Add Cookie | driver.manage().addCookie(new Cookie("key", "value")); |
| Delete Named | driver.manage().deleteCookieNamed("session"); |
| Delete All | driver.manage().deleteAllCookies(); |
13. TestNG Assertions
| Assertion | Code | Validates |
|---|---|---|
| Equals | Assert.assertEquals(actual, expected); | Equality |
| True | Assert.assertTrue(condition); | Boolean true |
| False | Assert.assertFalse(condition); | Boolean false |
| Not Null | Assert.assertNotNull(element); | Object exists |
| Not Empty | Assert.assertFalse(elements.isEmpty()); | Collection |
14. Timeouts
| Timeout | Code |
|---|---|
| Page Load | driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30)); |
| Script | driver.manage().timeouts().setScriptTimeout(Duration.ofSeconds(10)); |
| Implicit | driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); |
15. File Upload
| Action | Code |
|---|---|
| Simple Upload | driver.findElement(By.id("file")).sendKeys("/path/to/file.png"); |
16. Common Exceptions
| Exception | Cause | Fix |
|---|---|---|
NoSuchElementException | Wrong locator | Verify in DevTools |
StaleElementReferenceException | Page refreshed | Re-find element |
TimeoutException | Too slow | Explicit Wait |
ElementNotInteractableException | Hidden/disabled | Scroll + JS click |
ElementClickInterceptedException | Overlay blocking | Actions or JS click |
SDET Pro Tips (2026)
- WebDriverManager – Never download drivers manually:java
WebDriverManager.chromedriver().setup(); - Locator Priority: ID > CSS > XPath (10x speed difference)
- Always Explicit Waits, Never Thread.sleep()
- Screenshot on Failure – TestNG listener:java
@AfterMethod public void tearDown(ITestResult result) { if(ITestResult.FAILURE == result.getStatus()) { // Take screenshot } }
Interview questions
Beginner
Why prefer explicit waits over Thread.sleep()?
Explicit waits continue as soon as the condition is met; sleeps always wait the full time and still fail if too short.
What is the usual locator priority in Selenium?
ID first, then CSS selectors, then XPath when nothing else works.
How do you handle a dropdown in Selenium?
Wrap the select element in the Select class and choose by visible text, value or index.
Intermediate
How do you switch to an iframe?
Use driver.switchTo().frame(...) with the frame element, name or index, then switch back with defaultContent().
When would you use the Actions class?
For hover, drag and drop, keyboard combinations and other complex user interactions.
When should you use the JavaScript executor?
Only when WebDriver can’t perform the action, because it bypasses real user behaviour.
Advanced
How do you handle multiple windows or tabs?
Get window handles, switch to the new handle, act, then switch back to the original.
Why can mixing implicit and explicit waits cause problems?
Wait times can combine unpredictably, making failures slow and timeouts hard to reason about.
How do you make Selenium tests run in CI?
Use headless browsers or a remote grid, manage drivers automatically, and capture screenshots on failure.
Go deeper · QA Bash kit
SDET Interview Kit
Crack the coding and design rounds SDET interviews are built on.
3–6 yrs · 60 coding problems
₹599₹899
Rate this article
8.8/10 average · 34 ratings
Discussion
Be the first to weigh in
Your question or experience helps the next tester who reads this. Sign in and earn +25 XP for every approved comment or reply.
Written by
Community Wire at QA BashGurugram, India
He’s a builder of communities, a collector of questions, and a relentless challenger of assumptions. While others chase answers, he chases better questions. While others talk about the future of testing, he quietly helps create it.
Keep reading

