A pragmatic approach to QA and OPS

Selenium Interview Questions

  • Annotations used for TestNG?

Ans : The various annotations used by TestNG are :


TestNG annotations are described in full detail here.

  • Define method to read data from excel.

Ans : The method of ready from excel differs if you’re using Java or Python. Keeping this in mind, I have posted two separate posts on this. Here is the Python one.

  • What is an xpath? What is the use of xpath

Ans : Xpath : Xpath is a language that describes the way to locate and process elements in XML documents. It uses an addressing sequence based on path through the document’s logical structure of hierarchy.

In simple language, Xpath specifies a route to an element or a node based on certain specific nodes in an XML document.

Suppose your xml document looks like this

<xml version="1.0" encoding="UTF-8">

<title lang="en">Harry Potter/title>

<title lang="en">Learning XML/title>


Here bookstore is the root element, and book is the child of root. Suppose you want to get the title of all the books which are having language “en”, then we can use this


  • Types of locators by which we can recognize web elements.

Ans : We can recognize web elements on a webpage using six locating mechanisms.

Locating by using id :

Every element must have a unique id on a HTML page. As such they make a unique and reliable way to locate a specific element on a HTML page.  Plus all browsers have the methods to get objects on the page using their ids. As such locating by ids is the fastest method available.

<form name="loginForm">
Login Username: <input id="username" name="login" type="text">
Password: <input id="password" name="password" type="password">
<input name="login" type="submit" value="Login">

Suppose you want to select the username or password field, you can do it be using their ids like



Locating by name:

Generally elements are given unique ids in an HTML document. If ids are not present and we only have names, then we can also use them to select a webelement.

<form name="loginForm">
Login Username: <input id="username" name="login" type="text">
Password: <input id="password" name="password" type="password">
<input name="login" type="submit" value="Login"/>

Now I can select the username webelement using the name login given in the HTML.


The difference between name and id is that we don’t always unique names in a webpage. As such if there is another username field with the name “login“, using the above code for locating will return only the first available element in the webpage.

Locating By LinkText:

This locating mechanism is useful for selecting hyperlinks from a page.

<a href="signin.html">Sign In<a>

Now we can use the link text for this hyperlink by using the By.linkText() locator- like this

driver.findElement(By.linkText("Sign In"));

Locating By  Partial LinkText:

The above mentioned link can also be selected by using the partialLinkText() method.  This method allows us to select a hyperlink by only using a fraction of the linktext mentioned.


Locating By Xpath:

Xpath are a useful mechanism for locating webelements. If used wisely, Xpath produces very reliable and low maintenance, but if used incorrectly, it can cause a lot of headache. Consider this html block.

<table name="cart">
<tr id="item1">
<td class="name item">Mp3 Download: foobar.mp3</td>
<td class="name item">
<input name="qty" class="name item formfield disabled"</td>
<tr id="item2">
<td class="name item">Mp3 Player</td>
<td class="name item">
<input id="item2_quantity" name="qty" class="name item formfield required" type="text"></td>

We can use various xpath expressions to select elements. For example //input[@name=’qty’]  can be used to determine the name item from the first table row.


Since both item1 and item2 have name , to select the second item from item2 node, we need to have something else define the input attribute. Here we can take the use of class name and type such  as //input[contains(@class,’required’) and type=’text’]

driver.findElement(By.xpath("//input[contains(@class,'required') and type='text']"));

Xpaths are versatile but slow, especially in older versions of IE, which makes them prone to hate by many automation testers. Also you need to be very careful while handling xpaths in certain situations like while handling Scalable Vector Graphics (SVG). For example, refer this chart, the HTML make looks something like this

<div id="svgchart">
<svg xmlns="http://www.w3.org/2000/svg">
<path .../>

In cases like this, if you use xpath like this //svg then the script will throw this error ERROR org.openqa.selenium.NoSuchElementException: Unable to locate element This is because the svg is in a different namespace. To correct this we need to use xpath in a different way
//*[local-name()=’svg’ and namespace-uri()=’http://www.w3.org/2000/svg’%5D

Locating by CSS Selector:

We can also select elements on a webpage using the CSS attributes using the By.cssSelector() method

&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;table name="cart"&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;tr id="item1"&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;td class="label"&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;Mp3 Download: foobar.mp3&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;/td&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;td class="item"&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;input name="qty" class="formfield disabled" &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;/td&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;


&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;tr id="item2"&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;td class="label"&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;Mp3 Player&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;/td&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;td class="item"&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;input id="item2_quantity" name="qty" class="formfield required" type="text"&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;/td&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;



We can use the following css selector attributes to select the item with id “item2_quantity“,




Locating By Class Name:

We can also use class name in locating the elements. This is not a preferred method of finding elements and is more of a convenience mechanism. In the above HTML snippet, we can use the class name to locate the element


  • Difference in assert and verify.

Ans : Selenium gives two excellent methods for validating results. Assert and Verify. Both perform the same function, albeit the difference comes in what you decide to do in case your test fails.

Assert would halt the program execution, at the first failure. Means, Assert will halt the program execution at the point, when the first check fails. It would log the failure and would not do any subsequent checks.

Verify, on the other hand would not halt the program execution. If your program runs a verify method, then it will log the failure and you will be guaranteed that all the checks are executed.

  • Alternate method to click a button on a page.

Ans : Selenium gives two method to click on a web element. The click( ) and the submit( ) function. If you don’t use the click( ) method, you can use the submit( ) function to click on a web element (probably a button/link)

WebElement btn=driver.findElement(By.id("#btnid"));


  • How to verify if a check box is clicked on webpage or not.

Ans: Here is the code snippet for checking this – we use a Boolean variable to see if the isSelected( ) method returns true for the checkbox.

WebElement e= driver.findElement(By.id("#elementid"));

Boolean chkbox= false;








  • Handling alert pop up in Selenium.

Ans: We can handle alerts in Selenium using switch.to.alert( ) method (in Python it is switch_to.alert property).

Here is the a full post explaining how we can handle the different types of alerts. This is in Python.

  • How to launch IE /Chrome browser.

Ans : Internet Explorer and Google’s Chrome Browser can not be directly launched by Webdriver API, like we do for Firefox. It needs a slight change in how we open the browser. First we need to use the System.setProperty() method to set the path of the IE/Chrome driver file.

The InternetExplorerDriver or ChromeDriver object should only be created after the webdriver.ie.driver or webdriver.chrome.driver property is set.

In IE we do this as

System.setProperty("webdriver.ie.driver", "D:\\Eclipse Workspace\\Multi Browser\\IEDriverServer.exe"); WebDriver obj = new InternetExplorerDriver();

Same for Chrome as

System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe" );
ChDriver = new ChromeDriver();
System.out.println("Webpage title "+ChDriver.getTitle());
  • Performing right click using webdriver.

Ans: We can use the Actions class provided by the Selenium API to perform or simulate mouse right click.  We use a combination of Actions and Keys interface using the method contextClick()

WebDriver driver= new FirefoxDriver();


WebElement e=driver.findElement(By.id("#elementid"));

//create a new object of Actions class

Actions act= new Actions(driver);

//use this object to call the contextClick( ) method on webelement e and select the Open in New tab functionality using Down Keys.


driver.close( );

  • Drag and drop in webdriver.

Ans : The Actions class in selenium webdriver allows us to simulate a drag and drop feature present on many websites.

We can do this in three different ways-

  1. using the clickAndHold ( ) and moveToElement( ) methods
  2. using the dragAndDrop( ) method
  3. using the dragAndDropBy( ) method

WebElement from=driver.findElement(By.xpath("//*[@id='draggable']"));
WebElement to=driver.findElement(By.xpath("//*[@id='droppable']"));
Actions act=new Actions(driver);
//using clickAndHold

//using dragAndDrop
act.dragAndDrop(from, to).build().perform();

//using dragAndDropBy() using co-ordinates
act.dragAndDropBy(to, 300, 20);
catch(Exception e){

  • Method overloading in webdriver.

  • Downloading a file using webdriver.

  • Clicking on a menu item in a drop down menu.

  • Simulating browser forward and back actions

Ans : Navigation in between web pages using Selenium webdriver is done by using the method provided by the Webdriver interface. These methods are :

  1. back( )
  2. forward( )
  3. refresh( )
  4. to( )

An excellent explanation of all these methods is provided here in the Selenium’s official documentation.

Let us take a scenario to understand this . Head over to this website. We are going to simulate this scenario-

  1. Open Toolsqa.com website
  2. Click on About link ( On top navigation).
  3. Come back to Home page (Use ‘Back’ command).
  4. Again go back to About page (This time use ‘Forward’ command)
  5. Again come back to Home page (This time use ‘To’ command)
  6. Refresh the Browser (Use ‘Refresh’ command)

package testDemo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.ChromeDriver;

public class TestNav{
private WebDriver driver;
private String baseUrl;

public void testmain(){

//open the desired webpage

//opening ABOUT link

//navigate back to home page

//forward link to ABOUT

//come back to home back- using to() method

//refreshing a page

public void beforeTest(){
driver=new Chromedriver();

public void afterTest(){

If we’re using the Python bindings, we can do it using the corresponding methods. Here is the post that discusses it.

  • Getting Current Page URL

Ans : Selenium, Java bindings provides a specific method called  getCurrentUrl( ) method, which gives you the url of the page, the browser is currently looking at. The Python bindings,equivalent of this method is current_url property.

Read it in official Selenium documentation here.

  • Difference between “/” and “//” in defining xpaths.

Ans : We use two different kinds of slashes in defining xpaths- a single slash (/) and a double slash (//).

A single slash (/)

  1. Starts the document from the document node.
  2. Allows you to create absolute xpath expressions.

A double slash (//)

  1. Starts the document from the selection matching anywhere in document.
  2. Allows you to create relative xpath expressions.

eg- /html/body/p matches all the paragraph elements. Using a relative xpath expression, we can denote this using //p

  • Difference between findElement() and findElements() methods

Ans: The difference is very simple. findElement( ) is used to find a specific element-on a webpage. This method searches for a single webelement on a web page.

findElements( ) searches for more than one element on a web page.

A simple example : Suppose we want to display the link text of a single link -then for recognizing the link element, we would use the findElement( ) method. But, if we want to display the link text for all web links in a web page, then we would need to fetch many links at one time. So to recognize a list of web elements, we use the findElements( ) method.

  • Getting value from a text box.

Ans : We can get the value of a text box using the getAttribute( ) method provided by the Webdriver API.

Don’t confuse this with the getText( ) method available by the API. There is a fundamental difference between getAttribute( ) and getText( ).

getText( ) method is used for retrieving text values between two element nodes.


Here getText( )method will return Something

In case of a text-box, the value entered goes into the “value” attribute. So getText( ) would not work. We need to use something like this


This would return the value entered in the text box.

  • Languages supported by webdriver.

Ans : Currently webdriver implementations are available in the following languages :

  1. Java
  2. C#
  3. Python
  4. Perl
  5. Ruby
  6. PHP
  7. Javascript

Out of these PHP is not officially developed by Selenium.

  • Clear contents of textbox.

Ans : You can use clear() function to clear contents of a text box.

WebElement e=driver.findElement(By.id("masthead-search-term")); //search for youtube's search box

e.sendKeys("Pewdiepie"); // send search string
System.out.println("sent text");
e.clear(); //clear contents from textbox
System.out.println("clear text");

  • Limitations of Selenium.

Ans: Inspite of being a very handy tool for functional and regression testing, Selenium also has it’s limitations.

  1. Selenium supports testing of only web based applications. Windows applications are not tested using Selenium.
  2. Mobile applications also can not be tested directly using Selenium.
  3. Captcha code and bar code readers on websites can’t be tested using Selenium.
  4. Selenium can’t generate any test report on its own. You need third party extensions like TestNG and JUnit for generating such reports.
  5. Since Selenium is an open source tool, it has some limitations when coming to having support. In case of any issue, you are reliant solely on the community for help. There is no specific vendor support when it comes to Selenium.
  6. Apart from Selenium IDE, all other components of Selenium requires the tester to have some kind of prior programming experience.
  • How to invoke an application in browser using Selenium.

Ans: We have the get( ) method which is used to invoke an application or a webpage in a browser. The syntax is almost similar in Java and python.



In python, it is almost same



  • What is Selenium Grid?

Selenium GRID : Selenium Grid was first conceived by Paul Lightbody. Selenium Grid is a server that allows you to run tests-across machines, across different browsers, in parallel. So if you want your test execution to be on different platforms- across browsers, all running in the same time, then Selenium Grid is your tool. The perfect definition is mentioned in the Selenium Grid source page– it allows running your tests in a distributed test execution environment.

Selenium Grid Setup

  • Program to get number of frames in a webpage.

Ans : Here is the program for getting the number of iframes in a page. We need to use a list to store all the iframes we find and then get the size of the list.

List&lt;WebElement&gt;framelist = driver.findElements(By.tagname("iframe"));

int sizeofframe;

sizeofframe = framelist.size( );

System.out.println("Number of frames is " +sizeofframe);

  • Simulating scroll down to bottom of a page using selenium.

Scrolling down a window or to the bottom of the page can be done using JavaScriptExecutor  , which helps us execute native or plain JS commands within the Selenium code.

A simple example in Python would be to do this to scroll down to the bottom of the browser window

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

Another method, is to pass a really big integer value in the scrollTo parameter, so that the code takes you to the bottom of the viewport.

driver.execute_script("window.scrollTo(0, 10000);")

This can be implemented in Java as

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,10000)", "");

In case you want to execute this to a page, which keeps on scrolling down infinitely, then you can do this in this way given here.


  • Main package to be imported for selenium

It depends on the driver implementation. It may be different for Java implementation while it may be different for Python implementation.

For Selenium Python bindings, you just need to import the webdriver class from selenium as

from selenium import webdriver

For Selenium Java Bindings, you need to import the org.openqa.selenium packages and their classes to use in the Java code.

import org.openqa.selenium;
import org.openqa.selenium.By //this is a class of the above mentioned package


  • Check if an element is visible on a webpage.

Although this seems to be a familiar scenario which, looks like can be handled easily, but actually is dependent on a lot of things.

Selenium bindings give a method isDisplayed()[Java] , is_displayed()[Python] , isDisplayed()[Protractor] ,which checks if an element is displayedon the viewport of a browser, under consideration.

For example, in Python, you can do something like this


     print('Element is displayed')


     print('Element is not displayed')


If you check this answer given by me on SO, you will get an idea, that the above method is not full proof, and would fail in situations when your HTML has display attribute has value none.

In case the above method fails, you can check the size of the element to be not equal to 0, which can also help in verifying if the element is displayed on the viewport. You can do something like

driver.find_element_by_css('div#displayed').size !=0


  • Check if a button is enabled on a webpage.

This can be achieved by calling the isEnabled() or is_enabled() on the locator used for the button.


  • Checking a text is highlighted on a webpage. If not, then highlight the text.



  • Checking the color of element on a webpage – using RGB style.

Checking an element’s color on a webpage, is something that is one of the CSS attributes. So we need to get the CSS property color of the element to get the color of the element.

We can get this color using the getCssValue() method or the value_of_css_property() method in Python bindings to do the same.


For example, if we want to get the value of  color attribute, we can simply do this


For java, it would be something like


Not that the output of the result would be either in the RGBA format or in the hexadecimal format, depending upon how the css attribute has been coded in the CSS for the element.

  • Checking if a checkbox or radio button is selected or not.

You can check if a checkbox or radio button is selected or not using the isSelected()or is_selected() method of the Selenium webdriver bindings.

For example, for Python bindings, you can do something like this

if (driver.find_element_by_css_selector('#radiobtn').is_selected()):
print('This is if block')
print('This is else block. Element is not selected.')



  • Get title of a page.

The method is used to get the page title for Selenium Java bindings, while in case of Python bindings, it is an attribute and can be obtained as an attribute of the browser/driver.


For python, you can do this,


  • Getting height and width of a textbox- inbuilt method

To get the height and width of a particular web element, we need get the x,y pixels of the web element and then get the getSize() method to return the width and height of the element using the corresponding method

WebElement Image = driver.findElement(By.xpath("//img[@border='0']"));

        //Get width of element.
        int ImageWidth = Image.getSize().getWidth();
        System.out.println("Image width Is "+ImageWidth+" pixels");

       //Get height of element
        int ImageHeight = Image.getSize().getHeight();
        System.out.println("Image height Is "+ImageHeight+" pixels");&amp;amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;amp;gt;



In Python, the webdriver has properties .size and .location which can be used to get the height and width of the elements

e = driver.find_element_by_css_selector('div#id')

location = e.location()



  • Getting height and width of textbox using css attributes.

This can be done using the height and width css attributes. You can call the getCssValue() or the value_of_css_property() methods of the respective bindings and then get the value of these attributes after passing them as parameters

WebElement ele = driver.findElement(By.id('id'));

System.out.println("Height of element "+getCssValue('height'));

System.out.println("Width of element "+getCssValue('width'));


And in Python, you can do the same as

[/sourcecode language='python']

e = driver.find_element_by_id(‘id’)





  • Get the attributes of a webelement.

You need to call the getAttribute() method on the web element to get the attributes of the web element. For example, we can have a class attribute in our HTML for a web element, which can be obtained via

String s1 = driver.findElement(By.id("id")).getAttribute("class");



For Python, you need to call get_attribute() method to do the same.

[/sourcecode language='python']

e = driver.find_element_by_css_selector(‘div#id’)





  • Hover over mouse action using webdriver.


  • Use of getoptions() method

  • Use of deselect() method

  • WebElement is a class or interface.

  • Firefoxdriver is class or interface.

  • Super interface of webdriver.

  • Difference between close() and quit.

  • Implicit vs explicit wait with code

  • Capture screenshot of a webpage in webdriver.

  • Capture screenshot of a specific element in webdriver.

  • Maximising a window using selenium

  • Capture video of running scripts.

  • Verify response code 200.

  • Verify image using selenium.

  • Handle authentication in selenium.

  • Automating HTML5 video using selenium

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: