Add Selenium Java dependency:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.18.1</version>
</dependency>
A quick reference guide for essential Selenium WebDriver commands, locators, waits, and patterns in Java.
Add Selenium Java dependency:
|
Add WebDriverManager (optional, but recommended):
|
Add TestNG/JUnit dependency:
(Or JUnit) |
Basic project structure:
|
Create a new Maven project in your IDE (Eclipse, IntelliJ). |
Update Maven project ( |
Ensure Java Development Kit (JDK) is installed and configured. |
Basic Initialization:
|
Using WebDriverManager (Recommended):
|
Other Browsers:
|
Configure Browser Options:
|
Maximize Window:
|
Set Implicit Wait (Not recommended with Explicit Waits):
|
Navigate to URL |
|
Navigate Back |
|
Navigate Forward |
|
Refresh Page |
|
Get Current URL |
|
Get Page Title |
|
Get Page Source |
|
Close Current Window |
|
Quit Browser Session |
|
|
Locate element by its ID attribute. |
|
Locate element by its Name attribute. |
|
Locate element by its CSS class name. |
|
Locate elements by their HTML tag name. |
|
Locate anchor elements ( |
|
Locate anchor elements ( |
|
Locate elements using CSS Selectors (flexible & fast). |
|
Locate elements using XPath expressions (powerful, but potentially brittle). |
Find a single element:
Throws |
Find multiple elements:
Returns an empty list if no elements are found (does not throw exception). |
Find element within another element:
|
Find multiple elements within another element:
|
Click an element |
|
Type text into input field |
|
Clear text from input field |
|
Submit a form |
|
Get element text |
|
Get attribute value |
|
Get CSS property value |
|
Check if element is displayed |
|
Check if element is enabled |
|
Check if element is selected (checkbox/radio) |
|
Sets a default timeout for finding any element.
Sets the wait time to 10 seconds. |
Disable Implicit Wait:
|
Implicit waits poll the DOM at regular intervals until the element is found or the timeout expires. |
Waits for a specific condition to occur before proceeding.
Waits up to 10 seconds for element with ID ‘myElement’ to be visible. |
Common
|
Waiting for multiple elements:
|
Ignoring exceptions during wait:
|
Defines maximum wait time, polling interval, and exceptions to ignore.
Waits up to 30s, checking every 5s, ignores |
Fluent Wait is useful when you need different polling intervals or specific ignored exceptions for different waiting conditions. |
Switch to Alert |
|
Accept Alert (OK) |
|
Dismiss Alert (Cancel) |
|
Get Alert Text |
|
Send Keys to Prompt Alert |
|
Wait for Alert (using Explicit Wait) |
|
Switch by Index |
|
Switch by Name or ID |
|
Switch by WebElement |
|
Switch back to Parent Frame |
|
Switch back to Default Content (top level) |
|
Wait for Frame (using Explicit Wait) |
|
Get Current Window Handle |
|
Get All Window Handles |
|
Switch to Window/Tab |
|
Switch back to Original Window |
|
Open New Tab (Selenium 4+) |
|
Open New Window (Selenium 4+) |
|
Close Specific Window/Tab |
|
Use the
|
Perform a simple click using Actions:
|
Double Click:
|
Context Click (Right Click):
|
Mouse Hover:
|
Drag and Drop (element to element):
|
Drag and Drop (element by offset):
|
Send Keys using Actions (useful for special keys):
|
Combined Actions (Build and Perform):
|
Take a full page screenshot:
Requires Apache Commons IO dependency (
|
Take screenshot of a specific element (Selenium 4+):
|
Saving screenshot to Base64 string:
|
A design pattern where web pages are represented as classes. |
Basic Structure:
|
Using PageFactory (Alternative element initialization):
|
Using POM in tests (
|