DEV Community

Cover image for Selenium Java Cheat Sheet
Anne Quinkenstein
Anne Quinkenstein

Posted on • Updated on

Selenium Java Cheat Sheet


Selenium is open Source Automation Testing tool which is exclusively for webbased applications.



3 Components
  • Selenium Java Client Library (with Java, C++, ..)
  • Browser Driver(chromedriver, geckodriver(Firefox), msedgedriver,...)
  • Browser (Chrome, Firefox, Edge, Safari, ...)
  • Selenium Library (Client)........ <-JSON(over HTTP)->
  • Browser Driver (Server) ......... <-HTTP(over HTTP)->
  • Browser
  1. trigger Test -> Selenium (e.g. Java) code converts to JSON
  2. sends to BrowserDriver throught HTTP
  3. Browser drivers communicate with its respective browser: It executes the commands by interpreting JSON and gives response back to Browser Driver.
  4. Browser Driver wraps Browserresponse in Json and sents it back to Client.

Using the Webdriver API

Every automation Java work file starts with creating a reference of web browser.
Webdriver is the Interface. ChromeDriver( or other drivers) is the class that implements the interface (which is all of the methods defined in the interface)
Download selected driver, hint Selenium to the location

System.setProperty("", "//home//IdeaProjects//chromedriver");
Enter fullscreen mode Exit fullscreen mode

Create an object for the Class and make Class object reference to the Webdriver Interface which you want to implement.

WebDriver driver = new ChromeDriver();
Enter fullscreen mode Exit fullscreen mode

There are several methods that are available from the Webdriver interface. These methods are accessed using the instance variable driver.

Enter fullscreen mode Exit fullscreen mode

open && close URL in a Browser

//might be checked with
String url = driver.getCurrentUrl();
boolean result = driver.getPageSource().contains("String to find");
String title = driver.getTitle();
driver.close(); // closes only current window
driver.quit(); // closes all the windows opened by WebDriver instance

Enter fullscreen mode Exit fullscreen mode


driver.findElement("username")).sendKeys("This is my first code");
Enter fullscreen mode Exit fullscreen mode

Not every object might got an ID, className or name, than you can use Xpath or CSS selectors
if there are multipl values - Selenium identifies the first one

caution ID, Class

If it is an alpha numeric id, check if it varies on every refresh.
Classes should not have spaces- Compound classes will not be accepted

xpath Syntax

//tagName[contains(@attribute,'value')]  -  regEx
with index //*[@id=’login’]/ul/li[3]/a
css is not recommended, cause it counts hidden childs as well


traverse to sibling 
traverse back to Parent element from Child 
Enter fullscreen mode Exit fullscreen mode

more on Xpath Contains, Following Sibling, Ancestor & Selenium AND/OR

//contains in CSS Selectors is

CSS selector Syntax

tagName[Atrribute*='value'] - regEx

 driver.findElement(By.cssSelector("[class='datepicker-days'] th[class='next']")).click();

//contains is *

Enter fullscreen mode Exit fullscreen mode

check in Browser with cropath extention or the console with:
$("") - for css , $x("") or xpath

Multiple values - Selenium identifies the first one- Scans from top left
id, class, xpath, css selectors, linkedText
id: check if it is changing

Top comments (0)