QAPage – Software Testing Jobs, QA Resources & Careers

QAPage Navigation & Directory
Type any keyword to search live pages, tools, or press Search to query site-wide.

Selenium Notes for QA: Complete Guide for Beginners

Selenium WebDriver

Selenium Notes for QA, Selenium WebDriver is a tool used in software testing to automate web browsers. It helps testers check whether a website is working correctly without performing every action manually.
For example, instead of manually opening a website, entering a username and password, and clicking the Login button, Selenium can perform all these actions automatically.
Selenium Notes for QA

1. Introduction to Selenium

Selenium is an open-source tool used to automate web applications. It supports browsers such as Chrome, Firefox, Edge, and Safari.
Main components of Selenium:
  • Selenium WebDriver – controls the browser.
  • Selenium IDE – records and plays back browser actions.
  • Selenium Grid – helps run tests on different browsers and machines.
Example: Automatically open a shopping website and check whether the login page is working correctly.

2. Selenium WebDriver Setup

Before using Selenium, we need to set up the required software and tools.
Requirements:
  • Java or another supported programming language.
  • An IDE such as Eclipse or IntelliJ IDEA.
  • Selenium libraries.
  • A supported web browser, such as Chrome.
Modern Selenium can manage browser drivers automatically in many common setups through Selenium Manager.
Example: Install Java, create a Java project, add Selenium dependencies, and write your first automation script.

3. Opening and Closing Browsers

Selenium can open a browser, perform testing actions, and close the browser after testing.
Common commands:
  • driver.get() – opens a website.
  • driver.close() – closes the current browser window.
  • driver.quit() – closes all browser windows opened by that WebDriver session.
Example: Open Google Chrome, visit a website, perform a test, and close the browser.
driver.get("https://www.google.com");driver.quit();

4. Browser Navigation Commands

Navigation commands help testers move between web pages, return to previous pages, or reload a page.
Common commands:
  • driver.navigate().to() – opens a URL.
  • driver.navigate().back() – returns to the previous page.
  • driver.navigate().forward() – moves to the next page.
  • driver.navigate().refresh() – reloads the current page.
Example: Open a shopping website, visit a product page, return to the home page, and refresh it.

5. Finding Web Elements

A web element is any part of a webpage with which a user can interact.
Examples include:
  • Text boxes
  • Buttons
  • Links
  • Checkboxes
  • Dropdown menus
  • Images
Selenium uses locators to find these elements on a webpage.
For example, Selenium can locate a username field and enter a username into it.
driver.findElement(By.id("username")).sendKeys("tester");
Here, Selenium finds the element with the ID username and enters the text tester.

6. ID, Name, ClassName and TagName Locators

Locators help Selenium identify specific elements on a webpage.
Locator
Meaning
Example
id
Finds an element using its ID
By.id("login")
name
Finds an element using its name attribute
By.name("email")
className
Finds an element using a CSS class
By.className("submit-btn")
tagName
Finds elements using HTML tags
By.tagName("input")
Example: If a login page has an email field with id="email", Selenium can locate it using By.id("email").
Important: IDs are generally a good choice when they are unique and stable. A class or tag name may match multiple elements.

7. XPath — Absolute and Relative

XPath is a locator used to find elements in an HTML document. It is useful when an element does not have a suitable ID or name.
There are two common types:
Absolute XPath: Starts from the root of the HTML document and follows the complete path to the element.
Example:
/html/body/div/form/input
Absolute XPath is often fragile because a small change in the page structure can break it.
Relative XPath: Finds an element using its attributes, text, or relationship with other elements.
Examples:
//input[@id='email']
//button[text()='Login']
Relative XPath is generally more practical because it can locate elements without depending on the complete page structure.

8. CSS Selectors

CSS selectors are another way to locate elements using their IDs, classes, attributes, or relationships.
Examples:
#email
Finds an element with the ID email.
.submit-btn
Finds elements with the class submit-btn.
input[name='username']
Finds an input element whose name is username.
Example: Selenium can use a CSS selector to find the email field and enter a user’s email address.
CSS selectors are often concise and useful for locating elements with stable attributes.

9. Handling Text Boxes and Buttons

Text boxes allow users to enter information, while buttons perform actions such as Login, Submit, or Search.
Common Selenium methods:
  • sendKeys() – enters text.
  • click() – clicks an element.
  • clear() – removes existing text.
  • getText() – reads visible text from an element.
Example: On a login page, Selenium can enter an email address, enter a password, and click the Login button.
driver.findElement(By.id("email")).sendKeys("test@example.com");driver.findElement(By.id("login")).click();
The element locators must match the actual webpage.

10. Radio Buttons and Checkboxes

Radio buttons and checkboxes are form elements used to select options.
Radio buttons: Usually allow the user to select only one option from a group.
Example: Select Male or Female in a form.
Checkboxes: Allow users to select one or more options.
Example: Select multiple skills such as Java, Selenium, and SQL.
Useful Selenium methods:
  • click() – selects or deselects an element.
  • isSelected() – checks whether it is selected.
  • isDisplayed() – checks whether it is visible.
  • isEnabled() – checks whether it is enabled.
Example: Selenium selects a checkbox for accepting the terms and conditions before submitting a registration form.

11. Dropdowns Using the Select Class

A dropdown displays a list of options from which a user can select an item.
For standard HTML dropdowns created with the select tag, Selenium provides the Select class.
Common methods:
  • selectByVisibleText() – selects an option using its displayed text.
  • selectByValue() – selects an option using its value attribute.
  • selectByIndex() – selects an option using its position.
  • getFirstSelectedOption() – returns the currently selected option.
Example: Select Maharashtra from a state dropdown.
WebElement state = driver.findElement(By.id("state"));Select select = new Select(state);select.selectByVisibleText("Maharashtra");
Important: The Select class works with standard HTML select elements. Custom dropdowns made with other HTML elements need a different approach.

12. Handling Alerts and Pop-ups

Alerts are browser JavaScript messages that may ask the user to confirm an action or display information.
Common alert types:
  • Simple alert – displays a message and an OK button.
  • Confirmation alert – provides OK and Cancel options.
  • Prompt alert – asks the user to enter text.
Selenium uses switchTo().alert() to handle JavaScript alerts.
Common methods:
  • accept() – clicks OK.
  • dismiss() – clicks Cancel.
  • getText() – reads the alert message.
  • sendKeys() – enters text into a prompt alert.
Example: When a website asks, “Are you sure you want to delete this record?”, Selenium can accept or dismiss the confirmation.
Note that HTML pop-ups and browser permission dialogs may require different handling methods.

13. Handling Frames and Iframes

A frame or iframe displays another HTML document inside a webpage.
Selenium usually needs to switch into the relevant frame before interacting with its elements.
You can switch using:
  • Frame index.
  • Frame name or ID.
  • A WebElement representing the frame.
Example:
driver.switchTo().frame("paymentFrame");
After completing the actions inside the frame, return to the main page:
driver.switchTo().defaultContent();
Example: A payment page may display card details inside an iframe. Selenium must switch into that iframe before entering the card information.

14. Handling Multiple Windows and Tabs

Sometimes a website opens a new browser window or tab when a user clicks a link.
Selenium can switch between these windows using their window handles.
Common methods:
  • getWindowHandle() – gets the current window’s handle.
  • getWindowHandles() – gets the handles of all open windows in the session.
  • switchTo().window() – switches to a particular window.
Example: A user clicks a link to open a privacy policy in a new tab. Selenium switches to that tab, checks its content, and then returns to the original tab.
Important: A window handle identifies a window; it is not the same as the window’s title or URL.

15. Mouse Actions Using the Actions Class

The Actions class is used to perform advanced mouse interactions.
Common actions include:
  • moveToElement() – moves the mouse over an element.
  • click() – clicks an element.
  • doubleClick() – double-clicks an element.
  • contextClick() – performs a right-click.
  • dragAndDrop() – drags an element and drops it somewhere else.
Example: On an e-commerce website, moving the mouse over a category may display a submenu. Selenium can use moveToElement() to open the submenu and test its links.

16. Keyboard Actions

Selenium can simulate keyboard input to test how a webpage responds to user actions.
Examples include:
  • Enter
  • Tab
  • Escape
  • Backspace
  • Ctrl+A
  • Shift and other modifier keys
The Keys class provides keyboard key constants, and the Actions class can combine key presses with mouse actions.
Example: Selenium enters a search term into a search box and presses Enter to display the results.
driver.findElement(By.id("search"))      .sendKeys("Selenium WebDriver", Keys.ENTER);
This helps test keyboard navigation, form submission, and keyboard shortcuts.

17. Web Tables and Dynamic Tables

A web table displays information in rows and columns.
For example, an employee table might contain employee IDs, names, departments, and salaries.
Selenium can find table rows and cells using HTML elements such as:
  • table
  • tr – table row
  • th – header cell
  • td – data cell
Static table: Its data generally remains unchanged during the test.
Dynamic table: Its content may change when users search, filter, sort, or load additional records.
Example: Selenium searches for employee ID 105 and verifies that the correct employee name appears in the table.
For dynamic tables, testers may need to wait for updated content and locate the required row based on its actual data rather than a fixed row number.

18. Scrolling and JavaScriptExecutor

Some webpage elements are located below the visible screen area. Scrolling helps bring these elements into view.
Selenium can scroll using browser interactions, element actions, or JavaScript.
JavascriptExecutor allows Selenium to execute JavaScript in the browser.
Example: Scroll to the bottom of a webpage:
JavascriptExecutor js =  (JavascriptExecutor) driver;js.executeScript("window.scrollTo(0, document.body.scrollHeight);");
This scrolls the page to its bottom.
JavaScript can also be used to inspect page properties or interact with certain elements, but normal Selenium interactions are preferable when possible because they better represent user behavior.

19. Uploading and Downloading Files

File upload and download testing checks whether a website handles files correctly.
File upload: Selenium can upload a file by sending its full path to a file input element.
Example:
driver.findElement(By.id("fileUpload"))      .sendKeys("C:\\Test\\resume.pdf");
The file input locator and file path must be valid.
File download: Selenium can configure browser preferences to save downloaded files to a specified folder and then verify that the expected file was downloaded.
Example: Test a resume upload form by uploading a PDF and checking whether the website accepts it.
File dialogs and browser download behavior can differ by browser, operating system, and test environment.

20. Taking Screenshots

Screenshots capture the visible state of a webpage during testing.
They are useful for:
  • Recording test failures.
  • Reporting UI bugs.
  • Keeping evidence for test reports.
  • Comparing the expected and actual appearance of a page.
Selenium provides the TakesScreenshot interface.
Example: If the login button is missing, the tester can capture a screenshot and attach it to the bug report.
Screenshots are especially useful in automated test frameworks because they help developers understand what happened when a test failed.

21. Handling Calendars and Date Pickers

Date pickers allow users to select dates for bookings, appointments, registrations, and other activities.
Some date pickers use a standard HTML date input, while others use custom calendar widgets.
Common testing activities:
  • Select a particular date.
  • Move to the next or previous month.
  • Select a year.
  • Verify that past or invalid dates are disabled.
  • Check the date format.
Example: On a hotel booking website, Selenium selects 15 November 2026 as the check-in date and verifies that the correct date appears in the field.
The exact approach depends on how the calendar is built. Standard HTML date inputs and custom calendar widgets are handled differently.

22. Explicit, Implicit and Fluent Waits

Webpages may take time to load elements because of network delays, animations, or background operations. Waits help Selenium avoid interacting with elements too early.
There are three common types.
Implicit wait: Tells Selenium to wait for a specified time while locating elements if they are not immediately available.
Explicit wait: Waits for a specific condition, such as an element becoming visible or clickable.
Fluent wait: Allows you to define how often Selenium checks a condition and which exceptions it should ignore while waiting.
Wait type
Simple meaning
Implicit wait
Wait while trying to find elements
Explicit wait
Wait for a particular condition
Fluent wait
Customize the waiting condition and polling interval
Example: After clicking Login, Selenium waits until the dashboard becomes visible before checking the page.
Explicit waits are often preferred because they let testers wait for the exact condition required by a test. Avoid mixing implicit and explicit waits unnecessarily because it can lead to unpredictable waiting times.

23. StaleElementReferenceException and Other Common Exceptions

An exception is an error that occurs while Selenium is running a test.
Understanding common exceptions helps testers identify and fix automation problems.
Exception
Simple explanation
NoSuchElementException
Selenium cannot find the requested element.
StaleElementReferenceException
The element reference is no longer valid because the page or element changed.
TimeoutException
A required condition did not become true within the allowed time.
ElementNotInteractableException
The element cannot be interacted with in its current state.
ElementClickInterceptedException
Another element blocks the click.
NoSuchFrameException
The requested frame cannot be found.
NoSuchWindowException
The requested browser window is no longer available.
Example of a stale element: Selenium finds a product price, but the page refreshes the product list. The previously found element reference may no longer work.
How to handle it: Find the element again after the page updates, use appropriate waits, and make sure the page is in the expected state before interacting with it. Do not simply retry every exception without understanding its cause.

24. Cross-Browser Testing

Cross-browser testing checks whether a website works correctly in different browsers.
For example, a login form may work in Chrome but have a display or functionality issue in Firefox.
Selenium supports automation across browsers such as:
  • Google Chrome
  • Mozilla Firefox
  • Microsoft Edge
  • Safari, where supported by the environment
Example: Run the same login test in Chrome, Firefox, and Edge to check whether the login feature works consistently.
Cross-browser testing helps identify differences in browser behavior, rendering, and compatibility.

25. Browser Options and Configurations

Browser options allow testers to customize how a browser starts and behaves during automation.
Examples include:
  • Running a browser in headless mode, without displaying its normal window.
  • Setting browser preferences.
  • Configuring download directories.
  • Adding browser arguments.
  • Enabling or disabling certain browser features.
  • Configuring proxy settings where needed.
Example: A test suite may run Chrome in headless mode on a CI/CD server where no graphical desktop is available.
Browser options differ between Chrome, Firefox, and Edge, so the appropriate options class must be used for the selected browser.

Quick Revision: Selenium WebDriver

Topic
What you should remember
WebDriver
Controls the browser automatically
Locators
Find elements on a webpage
XPath and CSS
Locate elements using attributes and page structure
sendKeys()
Enters text
click()
Clicks an element
Select
Handles standard HTML dropdowns
Actions
Performs advanced mouse and keyboard interactions
switchTo()
Switches between frames, alerts, and windows
JavascriptExecutor
Executes JavaScript in the browser
Waits
Help synchronize tests with webpage behavior
Exceptions
Explain problems that occur during test execution
Cross-browser testing
Checks functionality across different browsers
Browser options
Customize browser startup and behavior
Learning tip: Practise these topics in order. Start with browser setup and basic locators, then learn form controls, frames, windows, mouse actions, and waits. After that, practise exceptions, screenshots, file handling, and cross-browser testing.
For official documentation and practical examples, visit Selenium at selenium.dev.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top