13 Sets of APIs Every Tester Must Know in Selenium

DZone 's Guide to

13 Sets of APIs Every Tester Must Know in Selenium

This post lists API commands that will be helpful when testing with Selenium, such as navigation commands and Get methods.

· DevOps Zone ·
Free Resource

Selenium, a test automation tool most famous for testing web applications, has gained much popularity in the market these days. There is no other tool in the market which comes close to it.

The reasons this open source tool became so popular are its multiple browser support, multiple language support, constant development, and improvement, with a large support community and much more.

In this tutorial, we will talk about 13 APIs and methods which every automation tester must know about are most frequently used. Basic to advanced level Selenium APIs will be covered below:

1. Navigation Commands


To navigate to a particular URL.



To navigate to a particular URL.


Note: There is no difference between above two commands- both are exactly same and call the exact same piece of code.


Takes us one back in the browsing history.



Takes us one forward in the browsing history.



Reloads the page.


2. Resizing Windows

Set Size

Sets the size of window; the unit used is pixels.

Dimension d = new Dimension(640, 640);

Maximizing the Window

To set the size of the window as full size according to the screen.


The two methods above are used to test the responsiveness of the application, i.e. how application elements render on different screen resolutions.

3. Deleting Cookies

To delete cookies in Selenium, the command below is used:


This will not actually go and delete the physical cookies file from your browser, but it bypasses these files so that cookies should not affect your browsing experience.

4. Closing the Browser


Closes the currently active window. 



Closes all the windows which were opened in that selenium session.


5. Different Get Methods


This method returns the URL of currently active page as a string.


This method returns the complete html source code of the page.


This method returns the title of the current page as a string.


This method returns the sessionId or windowHandle (used to uniquely identify a particular window) as a string.


This method returns a set of sessionIds or windowHandles of all opened windows.

6. Searching WebElements

To search for web elements on a web page below methods are used:


This method returns a web element which matches the mentioned criteria.

WebElement element = Driver.findElement(By.id("text-box"));


This method returns a list of web elements which matches the mentioned criteria.

List<WebElement> allElements = Driver.findElements(By.id("checkbox"));

7. Locators in Selenium

  • By.Id

  • By.Name

  • By.Tagname

  • By.CssSelector

  • By.Xpath

  • By.Classname

  • By.Linktext

  • By.PartialLinktext

For this topic, you must know how to identify locators or identifiers in Selenium.

8. Wait Commands in Selenium

One of the biggest challenges automation engineers face is synchronization between the application under test and Selenium commands. Selenium provides us three different wait commands:

Page Load Timeout

This is the maximum time Selenium waits for a page to load successfully on a browser. If the page takes more than this time, it will throw a timeout exception.

Implicit Wait

This is the maximum time Selenium code waits to interact with a web element before throwing an “Element not found" exception.

Explicit Wait

This wait can be considered a conditional wait and is applied to a particular Web Element with a condition. There are many conditions which can be applied using explicit wait.There are more in wait commands in Selenium which you can explore.

9. Frame Handling

Iframes, or Frames in HTML, are used to divide a web page into multiple sections or to display a web page within another web page, and to interact with any WebElement of a frame. First, we have to switch to that frame, then only WebElements can be identified. Switching to a frame is possible in three ways:






WebElement frameElement = Driver.findElement(By.className("demo-frame"));

10. Window Handling

When pages open up in multiple windows, we need to switch between these windows to interact with these pages. Selenium provides us with a method called  switch.To.Window() . 

Below is the code to switch to the first window:

childWindow = Driver.getWindowHandles().toArray()[1].toString();

11. Alert Handling

An alert is a small box that appears on the display screen with a message and an OK button. The message can be some information or a warning. Alerts can not be ignored. It needs to be accepted or rejected before you can proceed with your actions on the web page. It can only be accepted or rejected.

Alert alert = Driver.switchTo().alert(); //Switching to Alert on same web page

String messageOnAlert = alert.getText(); // Fetching message on Alert box in a String
Thread.sleep(5000); // Explicit wait of 5 seconds so that you can see Alert for sometime

alert.accept(); //Accepting an alert

12. Mouse Operations

To perform mouse operations like mouse hover, drag, and drop, right clicking or double clicking with Selenium actions class is used. 

Mouse Hover:

Actions action = new Actions(Driver);

WebElement mobileElement = Driver.findElement(By.linkText("Mobile & Accessories"));


Drag and Drop:

WebElement source = Driver.findElement(By.id("draggable"));

WebElement target = Driver.findElement(By.id("droppable"));

Actions action = new Actions(Driver);

action.dragAndDrop(source, target).build().perform();

13. Working With Dropdowns

To interact with a dropdown using Selenium, select class is used

WebElement dropElement = Driver.findElement(By.id("gh-cat"));

//Object instantiation for selecting values from dropDown
Select dropdown = new Select(dropElement);

//Select value from the  DropDown

Method in Select class:

  • selectByVisibleText("text")

  • selectByValue("value")

  • selectByIndex("value")

  • deselectByVisibleText("text")

  • deselectByValue("value")

  • deselectByIndex("value")

  • isMultiple() - a method with return type as boolean, true states that dropdown allows multiple selection whereas false states that only one value can be selected.

  • getOptions() - returns a list of web elements which are options of a dropdown

  • getAllSelectedOptions() -returns a list of all selected web elements which are options of a dropdown.

  • getFirstSelectedOption() - returns a web element, e.g. first selected option.

I hope you all learned something from this article. Please share your feedback. Happy learning!

devops, selenium, selenium api, test automation

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}