Beginner to Advanced
Selenium Cheat Sheet: Your
Ultimate Guide!
🌟 The Complete Selenium WebDriver & SearchContext Method Cheat Sheet!
🧑💻
✅ 1. get() 🌐
Declared in: WebDriver interface
Implemented by: RemoteWebDriver class
How to use: Create an object of the WebDriver interface.
Arguments: Takes a URL as a String .
Returns: void (does not return anything).
Purpose: Launches a website.
WebDriver driver = new ChromeDriver();
driver.get("<https://www.example.com>");
✅ 2. getTitle() 🏷️
Declared in: WebDriver interface
Implemented by: RemoteWebDriver class
How to use: Create an object of the WebDriver interface.
Arguments: None.
Returns: Title of the page as String type.
Purpose: Useful to get the title of the current page.
WebDriver driver = new ChromeDriver();
String title = driver.getTitle();
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 1
✅ 3. getPageSource() 📜
Declared in: WebDriver interface
Implemented by: RemoteWebDriver class
How to use: Create an object of the WebDriver interface.
Arguments: None.
Returns: Source code of the page as String type.
Purpose: Useful to get the source code of the current page.
WebDriver driver = new ChromeDriver();
String pageSource = driver.getPageSource();
✅ 4. getCurrentUrl() 🔗
Declared in: WebDriver interface
Implemented by: RemoteWebDriver class
How to use: Create an object of the WebDriver interface.
Arguments: None.
Returns: URL of the page as String type.
Purpose: Useful to get the URL of the current page.
WebDriver driver = new ChromeDriver();
String currentUrl = driver.getCurrentUrl();
✅ 5. getWindowHandle() 🖼️
Declared in: WebDriver interface
Implemented by: RemoteWebDriver class
How to use: Create an object of the WebDriver interface.
Arguments: None.
Returns: Handle value of the current browser window/tab as String type.
Purpose: Useful to get the handle value of the current browser window/tab.
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 2
WebDriver driver = new ChromeDriver();
String windowHandle = driver.getWindowHandle();
✅ 6. getWindowHandles() 🖼️🖼️
Declared in: WebDriver interface
Implemented by: RemoteWebDriver class
How to use: Create an object of the WebDriver interface.
Arguments: None.
Returns: Handle values of all open browser windows/tabs as Set<String> .
Purpose: Useful to get handle values of all open browser windows/tabs.
WebDriver driver = new ChromeDriver();
Set<String> handles = driver.getWindowHandles();
✅ 7. close() ❌
Declared in: WebDriver interface
Implemented by: RemoteWebDriver class
How to use: Create an object of the WebDriver interface.
Arguments: None.
Returns: void .
Purpose: Useful to close the current browser window/tab.
WebDriver driver = new ChromeDriver();
driver.close();
✅ 8. quit() 🚪
Declared in: WebDriver interface
Implemented by: RemoteWebDriver class
How to use: Create an object of the WebDriver interface.
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 3
Arguments: None.
Returns: void .
Purpose: Useful to close all browser windows/tabs.
WebDriver driver = new ChromeDriver();
driver.quit();
✅ 9. navigate() 🧭
Declared in: WebDriver interface
Implemented by: RemoteWebDriver class
How to use: Create an object of the WebDriver interface.
Arguments: None.
Returns: Object of Navigation interface.
Purpose: Useful to navigate through the browser (back, forward, refresh,
navigate to URL).
WebDriver driver = new ChromeDriver();
driver.navigate().to("<https://www.google.com>");
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
✅ 10. switchTo() 🔄
Declared in: WebDriver interface
Implemented by: RemoteWebDriver class
How to use: Create an object of the WebDriver interface.
Arguments: None.
Returns: Object of TargetLocator interface.
Purpose: Useful to move the focus of the driver to a specific frame, alert, or
active element on the current page.
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 4
i. switchTo().frame()
Purpose: Shifts the focus of the driver to a specific frame on the webpage.
Sub-Methods:
driver.switchTo().frame(String frameNameOrId) – Switch using the frame's name or
ID.
driver.switchTo().frame(int frameIndex) – Switch using the index (0-based).
driver.switchTo().frame(WebElement frameElement) – Switch using a WebElement
representing the frame.
driver.switchTo().parentFrame() – Switch back to the parent frame.
driver.switchTo().defaultContent() – Return to the top-level page content.
ii. switchTo().alert()
Purpose: Switches the focus to an alert box on the page.
Actions on Alerts:
.accept() : Clicks the "OK" button on the alert.
.dismiss() : Clicks the "Cancel" button on the alert.
.getText() : Retrieves the alert's text.
.sendKeys(String keysToSend) : Sends input to the alert box (for prompts).
Alert alert = driver.switchTo().alert();
System.out.println(alert.getText());
alert.accept(); // Dismissing an alert
alert.dismiss(); // Dismissing the alert
alert.sendKeys("Test Input"); // Sending input to a prompt alert
driver.switchTo().alert().accept(); // Accepting the alert
iii. switchTo().window()
Purpose: Switches the focus to a specific browser window or tab using its
handle.
Sub-Methods:
driver.switchTo().window(String windowHandle) – Switch to a window using its
handle.
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 5
driver.getWindowHandle() – Retrieve the current window's handle.
driver.getWindowHandles() – Retrieve handles for all open windows.
String mainWindow = driver.getWindowHandle(); // Get main window handl
e
for (String window : driver.getWindowHandles()) {
driver.switchTo().window(window); // Switch to a new window
if (!window.equals(mainWindow)) {
driver.close(); // Close the new window
}
}
driver.switchTo().window(mainWindow); // Return to the main window
iv. switchTo().activeElement()
Purpose: Switches the focus to the element that is currently active
(focused) on the page.
Method: driver.switchTo().activeElement()
WebElement activeElement = driver.switchTo().activeElement();
System.out.println("Active element is: " + activeElement.getTagName());
activeElement.sendKeys("Testing active element");
v. driver.switchTo().newWindow(WindowType)
Purpose: Automatically create and switch to a new tab or window in the
browser.
driver.switchTo().newWindow(WindowType.TAB); // Opens a new browser t
ab
driver.switchTo().newWindow(WindowType.WINDOW); // Opens a new bro
wser window
✅ 11. manage() ⚙️
Declared in: WebDriver interface
Implemented by: RemoteWebDriver class
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 6
How to use: Create an object of the WebDriver interface.
Arguments: None.
Returns: Object of Options interface.
Purpose: Useful to work with cookies, timeouts, browser window, and
browser logs.
i. Cookies
Purpose: Handle browser cookies.
Sub-Methods:
driver.manage().getCookies() – Retrieve all cookies.
driver.manage().getCookieNamed(String name) – Retrieve a specific cookie by its
name.
driver.manage().addCookie(Cookie cookie) – Add a new cookie to the browser.
driver.manage().deleteCookie(Cookie cookie) – Delete a specific cookie.
driver.manage().deleteCookieNamed(String name) – Delete a cookie by name.
driver.manage().deleteAllCookies() – Clear all cookies from the browser.
ii. Timeouts
Purpose: Define time limits for browser operations.
Sub-Methods:
driver.manage().timeouts().implicitlyWait(Duration duration) – Set the time WebDriver
waits when searching for elements.
driver.manage().timeouts().pageLoadTimeout(Duration duration) – Set the time limit for a
page to load.
driver.manage().timeouts().scriptTimeout(Duration duration) – Set the time limit for script
execution.
iii. Window Management
Purpose: Manage browser window behavior.
Sub-Methods:
driver.manage().window().maximize() – Maximize the browser window.
driver.manage().window().minimize() – Minimize the browser window.
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 7
driver.manage().window().fullscreen() – Open the browser in full-screen mode.
driver.manage().window().getSize() – Retrieve the current size of the browser
window.
driver.manage().window().getPosition() – Retrieve the current position of the
browser window.
driver.manage().window().setSize(Dimension dimension) – Set a specific size for the
browser window.
driver.manage().window().setPosition(Point point) – Set the position of the browser
window.
iv. Logs (Available in some browsers and for advanced debugging)
Purpose: Retrieve logs for debugging.
Sub-Methods:
driver.manage().logs().get(String logType) – Retrieve logs of a specific type (e.g.,
"browser", "driver").
driver.manage().logs().getAvailableLogTypes() – Retrieve the list of available log
types.
12. findElement(By) 🔍
Declared in: SearchContext interface
Implemented by: RemoteWebDriver class
How to use: The SearchContext interface is the parent of the WebDriver
interface (inheritance).
Arguments: Takes a By class object.
Returns: WebElement interface.
Purpose: Useful to locate an element in the page source.
Sub-Methods (for Locating Elements):
By ID: driver.findElement(By.id("element_id"))
By Name: driver.findElement(By.name("element_name"))
By XPath: driver.findElement(By.xpath("xpath_expression"))
By CSS Selector: driver.findElement(By.cssSelector("css_selector"))
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 8
By Class Name: driver.findElement(By.className("class_name"))
By Tag Name: driver.findElement(By.tagName("tag_name"))
By Link Text: driver.findElement(By.linkText("link_text"))
By Partial Link Text: driver.findElement(By.partialLinkText("partial_text"))
✅ 13. findElements(By) 🔍🔍
Declared in: SearchContext interface
Implemented by: RemoteWebDriver class
How to use: The SearchContext interface is the parent of the WebDriver
interface (inheritance).
Arguments: Takes a By class object.
Returns: List<WebElement>
Purpose: Useful to locate and collect one or more matched elements in the
page source.
List<WebElement> elements = driver.findElements(By.className("example
Class"));
✅ 14. click() 🖱️
Declared in: WebElement interface
Implemented by: RemoteWebElement class
How to use: Use findElement() to locate an element, then invoke click() on that
element.
Arguments: None.
Returns: void .
Purpose: Simulates a click action on a web element (such as a button or
link).
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 9
WebElement element = driver.findElement(By.id("submitButton"));
element.click();
✅ 15. sendKeys() ⌨️
Declared in: WebElement interface
Implemented by: RemoteWebElement class
How to use: Use findElement() to locate an input field, then invoke sendKeys() to
type text into it.
Arguments: Takes a String value that will be sent to the input element.
Returns: void .
Purpose: Simulates typing text into a form field.
WebElement inputField = driver.findElement(By.name("username"));
inputField.sendKeys("testuser");
✅ 16. clear() ✂️
Declared in: WebElement interface
Implemented by: RemoteWebElement class
How to use: Use findElement() to locate an input field, then invoke clear() to
clear the existing text in the field.
Arguments: None.
Returns: void .
Purpose: Clears the text inside an input field.
WebElement inputField = driver.findElement(By.name("username"));
inputField.clear();
✅ 17. isDisplayed() 👀
Declared in: WebElement interface
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 10
Implemented by: RemoteWebElement class
How to use: Use findElement() to locate an element, then invoke isDisplayed() to
check if it is visible on the page.
Arguments: None.
Returns: boolean – true if the element is visible; false otherwise.
Purpose: Determines whether an element is visible in the browser window.
WebElement element = driver.findElement(By.id("submitButton"));
boolean isVisible = element.isDisplayed();
✅ 18. isEnabled() 💪
Declared in: WebElement interface
Implemented by: RemoteWebElement class
How to use: Use findElement() to locate an element, then invoke isEnabled() to
check if it is enabled (interactive).
Arguments: None.
Returns: boolean – true if the element is enabled; false if it's disabled.
Purpose: Checks if an element (such as a button or input field) is enabled
and can be interacted with.
WebElement element = driver.findElement(By.id("submitButton"));
boolean isEnabled = element.isEnabled();
✅ 19. isSelected() ✅
Declared in: WebElement interface
Implemented by: RemoteWebElement class
How to use: Use findElement() to locate an element, then invoke isSelected() to
check if the element is selected (for checkbox, radio buttons, etc.).
Arguments: None.
Returns: boolean – true if the element is selected; false if it is not.
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 11
Purpose: Checks if an element is selected, for example, a checkbox or
radio button.
WebElement checkbox = driver.findElement(By.id("acceptTerms"));
boolean isSelected = checkbox.isSelected();
✅ 20. getText() 📝
Declared in: WebElement interface
Implemented by: RemoteWebElement class
How to use: Use findElement() to locate an element, then invoke getText() to
retrieve the visible text of that element.
Arguments: None.
Returns: String – the visible text of the element.
Purpose: Extracts the text content from an element.
WebElement label = driver.findElement(By.id("welcomeMessage"));
String text = label.getText();
✅ 21. getAttribute() 🏷️
Declared in: WebElement interface
Implemented by: RemoteWebElement class
How to use: Use findElement() to locate an element, then invoke getAttribute() to
retrieve the value of a specified attribute of the element.
Arguments: Takes the attribute name (e.g., "href", "value") as a String .
Returns: String – the value of the specified attribute.
Purpose: Retrieve the value of an attribute from an element.
WebElement inputField = driver.findElement(By.id("username"));
String value = inputField.getAttribute("value");
✅ 22. wait() ⏳
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 12
Declared in: WebDriverWait class
Implemented by: WebDriverWait class
How to use: Creates an instance of WebDriverWait to wait for a certain
condition to be met before proceeding.
Arguments: Takes WebDriver instance and Duration (time to wait) as
parameters.
Returns: WebDriverWait – allowing chaining of conditions.
Purpose: Waits for a certain condition (e.g., element visibility, presence) to
be true before continuing with the test.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLo
cated(By.id("submitButton")));
✅ 23. WebDriverException ⚠️
Declared in: WebDriverException class
How to use: When performing actions like clicking or navigating, this
exception is thrown if WebDriver encounters an issue.
Arguments: None.
Returns: Throws an exception.
Purpose: Handles unexpected errors during WebDriver operations, such as
element not found, invalid element state, or timeout errors.
try {
WebElement element = driver.findElement(By.id("nonExistentElement"));
} catch (WebDriverException e) {
System.out.println("Error encountered: " + e.getMessage());
}
✅ 24. Alert 🚨
Declared in: Alert interface
Implemented by: RemoteWebDriver class
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 13
How to use: Use switchTo().alert() to handle browser alerts such as pop-ups,
confirmation dialogs, or prompt dialogs.
Arguments: None.
Returns: Alert – the alert object that can be interacted with.
Purpose: Handle pop-up alert boxes (accept, dismiss, retrieve text, etc.).
Alert alert = driver.switchTo().alert();
alert.accept(); // Accept an alert
alert.dismiss(); // Dismiss an alert
alert.getText(); // Get the text from an alert
alert.sendKeys("Some text"); // Send input to prompt alerts
✅Windows 🪟
25. Window Handles & Switching Between Multiple
Declared in: WebDriver interface
Implemented by: RemoteWebDriver class
How to use: Use getWindowHandles() to retrieve handles for all open windows
and switchTo().window() to switch between them.
Arguments: Takes the window handle (a String ) as an argument to switch
between windows.
Returns: void .
Purpose: This allows you to handle and switch between multiple browser
windows or tabs in Selenium.
i. Switching Between Windows
// Store the current window handle
String mainWindowHandle = driver.getWindowHandle();
// Iterate through all open windows
for (String windowHandle : driver.getWindowHandles()) {
driver.switchTo().window(windowHandle); // Switch to the new window
if (!windowHandle.equals(mainWindowHandle)) {
// Close the new window if needed
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 14
driver.close();
}
}
// Switch back to the main window
driver.switchTo().window(mainWindowHandle);
ii. Handle New Window with driver.switchTo().newWindow()
// Open a new tab
driver.switchTo().newWindow(WindowType.TAB);
// Open a new window
driver.switchTo().newWindow(WindowType.WINDOW);
✅ 26. Action Class - Performing Complex User Interactions 🖱️
Declared in: Actions class
Implemented by: Actions class
How to use: Use Actions to simulate complex user interactions like mouse
movements, key presses, drag and drop, etc.
Arguments: Takes WebDriver object and the actions you want to perform.
Returns: Actions object that can be chained with other actions.
Purpose: Enables the automation of complex user interactions such as
hover, double-click, drag-and-drop, etc.
i. Performing a Hover Action
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.id("hoverElement"));
actions.moveToElement(element).perform(); // Hover over the element
ii. Performing a Double-Click Action
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.id("doubleClickElement"));
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 15
actions.doubleClick(element).perform(); // Double-click on the element
iii. Drag and Drop Action
Actions actions = new Actions(driver);
WebElement source = driver.findElement(By.id("source"));
WebElement target = driver.findElement(By.id("target"));
actions.dragAndDrop(source, target).perform(); // Drag source to target
✅ 27. Fluent Wait ⏳
Declared in: FluentWait class
Implemented by: FluentWait class
How to use: FluentWait is a more flexible waiting mechanism that allows
you to define the frequency of checking for a condition.
Arguments: Takes WebDriver and Duration for timeouts and polling frequency.
Returns: FluentWait object.
Purpose: FluentWait can be used to define waiting conditions with the
ability to configure how often the condition should be checked.
FluentWait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(30)) // Max time to wait
.pollingEvery(Duration.ofSeconds(5)) // Poll every 5 seconds
.ignoring(NoSuchElementException.class); // Ignore specific exceptions
// Wait until a specific condition is true
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLo
cated(By.id("someElement")));
✅🖥️ 28. JavaScriptExecutor - Executing JavaScript in WebDriver
Declared in: JavascriptExecutor interface
Implemented by: RemoteWebDriver class
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 16
How to use: Use JavascriptExecutor to execute custom JavaScript code in the
context of the current page.
Arguments: Takes a String containing JavaScript code, and optionally an
array of arguments.
Returns: Object – result of the JavaScript execution.
Purpose: Useful for scenarios where WebDriver's standard actions might
not be enough, and JavaScript can be used to execute more complex
actions.
i. Executing JavaScript to Click an Element
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.id("submitButton"));
js.executeScript("arguments[0].click();", element); // Click using JavaScrip
t
ii. Executing JavaScript to Scroll the Page
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0, 250);"); // Scroll down 250px
✅ 29. Explicit Wait ⏳
Declared in: WebDriverWait class
Implemented by: WebDriverWait class
How to use: Use WebDriverWait with ExpectedConditions to wait for a specific
condition to occur before proceeding.
Arguments: Takes WebDriver and Duration for maximum wait time.
Returns: The object that corresponds to the expected condition.
Purpose: Waits for an element or condition to be true (e.g., visibility of an
element, clickability, etc.).
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLo
cated(By.id("submitButton")));
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 17
✅ 30. TakesScreenshot - Capturing Screenshots 📸
Declared in: TakesScreenshot interface
Implemented by: RemoteWebDriver class
How to use: Use TakesScreenshot to capture a screenshot of the current
browser state.
Arguments: None.
Returns: File – the screenshot file.
Purpose: Captures a screenshot of the current browser page, typically
used for debugging and reports.
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.
FILE);
File destinationFile = new File("path/to/screenshot.png");
FileUtils.copyFile(screenshot, destinationFile); // Save the screenshot
Let me know if you'd like more Selenium concepts or examples!
Beginner to Advanced Selenium Cheat Sheet: Your Ultimate Guide! 18