Useful Selenium WebDriver Code Snippets

October 5, 2017 Appirio

By: Niza Khunger

Manual testing comes with many of challenges, such as more investment in human resource, the final product can be prone to have errors, and delays in work. Automation testing is best suited for such difficult situations. And Selenium is the most widely used automation testing tool to test dynamic web applications. If you’re new to the world of automation testing, Selenium is “a software testing framework for web applications.”

In this blog, we would look at all Java snippets, that come in handy when you run your first automated WebDriver script. WebDriver is an interface that doesn’t need a special server to execute tests — instead, it just opens up a browser instance. WebDriver can be implemented on InternetExplorerDriver, FirefoxDriver, ChromeDriver, HtmlUnitDriver, OperaDriver and RemoteWebDriver.

  1. Switching between windows
    On initiation of WebDriver object, a unique alpha-numeric identification is assigned to each window, popularly known as “window handle”. This code is then used to switch the control among several windows.











  1. Using screenshots
    When we execute large numbers of test scripts, test fails will occur, and we’ll need to check why those tests have failed. Capturing screenshots is immensely important during this step, because it helps to debug and identify the problem.


  1. Working with elements inside an iFrame
    Before you try searching for the elements within the iFrame,  you need to switch the focus of your Selenium software to target the specific iFrame in which your desired element was present. This can be handled by using ID, Index, or WebElement.

1. By ID:
driver.switchTo().frame(“frame1”); // Select The Frame 1 driver.switchTo().frame(“frame2”); // Select the Frame 2

2. By Index:
driver.switchTo().frame(0); // Select The Frame with Index 0

3. By WebElement: driver.switchTo().frame(driver.findElements(By.tagName(“iframe”).get(0));

Note: To return to the main or parent iFrame, use these:

1. To Parent iFrame:

2. To Main iFrame:

  1. Handling pop-ups

    You can handle most pop-up windows or alert boxes using the functions here:

1. accept() – It auto-clicks on the “Okay” button as soon as the pop-up window appears.
2. dismiss() – It auto-clicks on the “Cancel” button as soon as the pop-up window appears.
3. getText() – It auto-returns the text displayed on the alert box.
4. sendKeys(String valueOfString) – It auto-enters the string into the alert box.

Alert alert = driver.switchTo().alert();

  1. Navigating back and forth in the browser

    The navigate interface exposes the ability to move backward and forward in the                                    browser’s history. Suppose there are two pages Page A and Page B(Page A -> Page B):

1. You can navigate back to Page A after reaching Page B, by using back().
2. After that, you can use forward() because at that time Page B must be in your browser history, so it will navigate to Page B.




  1. Javascript in Selenium

   For the execution of Javascript in Selenium within the browser, you need to use an interface              known as JavascriptExecutor. By using this interface, you can interact directly with the page. Let’s    assume some scenarios where JavascriptExecutor does the job perfectly:

1.The element is hidden from the web page, and to overcome        “ElementNotVisibleException” exception.
2. Scrolling anywhere on the webpage.

  Before you do any of this, you’ll need to import the JavascriptExecutor package.

  1. Some Other helpful Tips

     Here are some other well-known snippets that can be used in many scripts:

  • Current URL – To launch current URL
  • Refresh Page – refresh() is the most widely used command that refreshes the current page after execution
  • isDisplayed – It checks the presence of WebElement.
  • CheckBox – It checks the checkbox is selected i.e true.

Always remember, WebDriver communicates to the web browser locally, whereas RemoteWebDriver extends support to WebDriver by running tests on a separate machine. It requires the Selenium server to run on the same, or a “remote” machine. When the server receives a request, it starts searching for the machines which match the requirements, and then run the tests.

Previous Article
Building Native Mobile Apps with React Native
Building Native Mobile Apps with React Native

By Sugam Kalra We are living in the era of smartphones, where mobile has become an indispensable part of ou...

Next Article
Getting the most out of G Suite Enterprise
Getting the most out of G Suite Enterprise

By: Cynthia Lipkovitch G Suite is offered by Google in three different editions: Basic, Business, and Enter...