Tuesday, 29 October 2013

Selenium Web Driver Interview Questions and Answers in Java

On this page You will find most frequently asked interview questions on Selenium Web Driver in Java.
Please note that if you are using selenium web driver in different languages like .net, perl etc, answers will be same except few syntax differences.
Most of these selenium interview questions have been asked in big IT companies like Infosys, Cognizant, Wipro, TCS, CTS, Syntel, Sungard, Mindtree, hexaware, amdocs, amazon, HSBC, Persistant, Capgemini, Tech Mahindra, Mphasis, Cybage, Accenture, Patni, Igate, Synechron, Zensar, KPIT, L & T infotech etc.

You may also like to read below topics.
  1. Press key in Selenium Webdriver in Java
  2. Read Table Data in Selenium Webdriver in Java
  3. Take the screenshot in Selenium Webdriver in Java
  4. Execute JavaScript in Selenium Webdriver in Java
  5. Handle multiple browser windows in Selenium Webdriver in Java
  6. Double-Click on the Element in Selenium Webdriver in Java
  7. Exceptions in Selenium Webdriver in Java
  8. Synchronization in Selenium Webdriver in Java
If you are staying in India, USA, UK, Australia, Germany and looking for a job in selenium, these interview questions and answers will be very useful.

If you want to ask selenium questions, you can do so by posting the comment below.

What do you think on above selenium topic. Please provide your inputs and comments. Thanks

What are the common exceptions that might occur in selenium webdriver in Java?

Here is the list of common exceptions in Java in selenium web driver.

  1. Element is not clickable at point (x, y). 
  2. No Such Element - for Frame related issue
  3. The driver executable does not exist. Driver executable must be set by the webdriver.ie.driver system property. The path to the driver executable must be set by the webdriver.chrome.driver system property
  4. Stale Element Reference - This happens when new element comes in ajax
You may also like to read below topics.




  1. Press key in Selenium Webdriver in Java
  2. Read Table Data in Selenium Webdriver in Java
  3. Take the screenshot in Selenium Webdriver in Java
  4. Execute JavaScript in Selenium Webdriver in Java
  5. Handle multiple browser windows in Selenium Webdriver in Java
  6. Double-Click on the Element in Selenium Webdriver in Java
  7. Exceptions in Selenium Webdriver in Java
  8. Synchronization in Selenium Webdriver in Java

What do you think on above selenium topic. Please provide your inputs and comments. Thanks

How to get the value selected in list box in selenium web driver in Java?

Example - Below example demonstrates how we can select the value from the combo box on the web page using selenium webdriver.

//import the required packages and classes.

import java.io.File;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;




@SuppressWarnings("unused")
public class OpenGoogle {
    
public static void main(String [] arg) 
{

//set the path of the chrome driver exe file

System.setProperty("webdriver.chrome.driver", "C:\\Selenuim\\chromedriver2.8.exe");

//create the new instance of the chrome browser
WebDriver driver =  new ChromeDriver();


try{

//set the implicit and page load time outs  

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS);

//navigate to given url
driver.get("https://www.google.co.in/preferences");

//Maximize the browser window
driver.manage().window().maximize();

 //identify the element using id  
WebElement e =  driver.findElement(By.id("abc"));
Select comboBox = new Select(e);

//get the value selected in drop down using getAttribute method
value =  comboBox.getFirstSelectedOption().getAttribute("value");

System.out.println("Value selected is " + value );



Thread.sleep(2000);

}

catch(Exception e){
 System.out.println("Exception - > " + e.toString());
 }
 finally{
  driver.close();
  driver.quit();
 }
} //main function ends
 
}//class ends


You may also like to read below topics.




  1. Press key in Selenium Webdriver in Java
  2. Read Table Data in Selenium Webdriver in Java
  3. Take the screenshot in Selenium Webdriver in Java
  4. Execute JavaScript in Selenium Webdriver in Java
  5. Handle multiple browser windows in Selenium Webdriver in Java
  6. Double-Click on the Element in Selenium Webdriver in Java
  7. Exceptions in Selenium Webdriver in Java
  8. Synchronization in Selenium Webdriver in Java

What do you think on above selenium topic. Please provide your inputs and comments. Thanks

How to read data from web table in selenium web driver in Java?

Example - Below example in Java demonstrates how we can read the value from the table in web page using selenium webdriver API in Jav.

import java.io.File;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;



@SuppressWarnings("unused")
public class OpenGoogle {
    
public static void main(String [] arg) 
{

//set the path of the chrome driver exe file

System.setProperty("webdriver.chrome.driver", "C:\\Selenuim\\chromedriver2.8.exe");

//create the new instance of the chrome browser
WebDriver driver =  new ChromeDriver();


try{

//set the implicit and page load time outs  

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS);

//navigate to given url
driver.get("https://www.google.co.in/preferences");

//Maximize the browser window
driver.manage().window().maximize();

//get the collection of all rows from the table
List<WebElement> tr =  driver.findElements(By.xpath("//table//tr"));

//get the collection of all cells from the first row in the table
List<WebElement> cells = tr.get(1).findElements(By.tagName("td"));
   
 for (WebElement cell : cells)
 {
    //Here we are printing the contents of table cells from row 1
    System.out.println( cell.getText()  );    
 }

Thread.sleep(2000);

}

catch(Exception e){
 System.out.println("Exception - > " + e.toString());
 }
 finally{
  driver.close();
  driver.quit();
 }
} //main function ends
}//class ends

You may also like to read below topics.
  1. Press key in Selenium Webdriver in Java
  2. Read Table Data in Selenium Webdriver in Java
  3. Take the screenshot in Selenium Webdriver in Java
  4. Execute JavaScript in Selenium Webdriver in Java
  5. Handle multiple browser windows in Selenium Webdriver in Java
  6. Double-Click on the Element in Selenium Webdriver in Java
  7. Exceptions in Selenium Webdriver in Java
  8. Synchronization in Selenium Webdriver in Java

What do you think on above selenium topic. Please provide your inputs and comments. Thanks

How to perform mouseover / mousehover action in selenium webdriver in java?


 Here driver is the WebDriver object and e is the element on which you want to perform mouse over action

Example -

import java.io.File;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;



@SuppressWarnings("unused")
public class OpenGoogle {
    
public static void main(String [] arg) 
{

//set the path of the chrome driver exe file

System.setProperty("webdriver.chrome.driver", "C:\\Selenuim\\chromedriver2.8.exe");

//create the new instance of the chrome browser
WebDriver driver =  new ChromeDriver();


try{

//set the implicit and page load time outs  

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS);

//navigate to given url
driver.get("https://www.google.co.in/preferences");

//Maximize the browser window
driver.manage().window().maximize();

//find the element on which you want to mouse hover
WebElement e =  driver.findElement(By.xpath("//*[@id='Trade_Summary1_1']/table/thead/tr"));

//we can perform complex operations like mouse over using Actions class 
 Actions builder = new Actions(driver);
 Action hoverAction = builder.moveToElement(e).build();

//perform the mouse over action in selenium webdriver
 hoverAction.perform();


Thread.sleep(2000);

}

catch(Exception e){
 System.out.println("Exception - > " + e.toString());
 }
 finally{
  driver.close();
  driver.quit();
 }
} //main function ends

}//class ends

You may also like to read below topics.
  1. Press key in Selenium Webdriver in Java
  2. Read Table Data in Selenium Webdriver in Java
  3. Take the screenshot in Selenium Webdriver in Java
  4. Execute JavaScript in Selenium Webdriver in Java
  5. Handle multiple browser windows in Selenium Webdriver in Java
  6. Double-Click on the Element in Selenium Webdriver in Java
  7. Exceptions in Selenium Webdriver in Java
  8. Synchronization in Selenium Webdriver in Java
What do you think on above selenium topic. Please provide your inputs and comments. Thanks

How to check the check box in selenium web driver in java?

You can see if checkbox is checked or not using below code.

Example -

//import the required packages and classes.

import java.io.File;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;



@SuppressWarnings("unused")
public class OpenGoogle {
    
public static void main(String [] arg) 
{

//set the path of the chrome driver exe file

System.setProperty("webdriver.chrome.driver", "C:\\Selenuim\\chromedriver2.8.exe");

//create the new instance of the chrome browser
WebDriver driver =  new ChromeDriver();


try{

 //set the implicit and page load time outs  

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS);

//navigate to given url
driver.get("https://www.google.co.in/preferences");

//Maximize the browser window
driver.manage().window().maximize();


boolean x  = driver.findElement(By.id("myid")).isSelected();

//if check box is not selected, select it using click method.
if (x == false)
driver.findElement(By.id("myid")).click();

//This is how you can check the checkbox.

//Using same logic, you can uncheck or deselect the checkbox as displayed below.

boolean x  = driver.findElement(By.id("myid")).isSelected();

if (x == true)
driver.findElement(By.id("myid")).click();

Thread.sleep(2000);

}

catch(Exception e){
 System.out.println("Exception - > " + e.toString());
 }
 finally{
  driver.close();
  driver.quit();
 }
} //main function ends

}//class ends

What do you think on above selenium topic. Please provide your inputs and comments. Thanks

How to click the link or web button in selenium web driver in java?


We can click any link or web button or any web element in a webpage by using click() method provided in Selenium webdriver API in JAVA.

Example -

WebElement e = driver.findElement(By.id("myid"));
e.click();

Above code will click on the element (link/button etc) whose id attribute value is - myid


Complete Example to click on button or link  using Selenium Webdriver in Java

Below example shows how we can click on any element in selenium webdriver in Java.

//import the required packages and classes.

import java.io.File;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

@SuppressWarnings("unused")
public class OpenGoogle {
    
public static void main(String [] arg) 
{

//set the path of the chrome driver exe file

System.setProperty("webdriver.chrome.driver", "C:\\Selenuim\\chromedriver2.8.exe");

//create the new instance of the chrome browser
WebDriver driver =  new ChromeDriver();


try{

//set the implicit and page load time outs  

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS);

//navigate to given url
driver.get("https://www.google.co.in/preferences");

//Maximize the browser window
driver.manage().window().maximize();

//identify the button or link element using id
WebElement e = driver.findElement(By.id("myid"));

//enter value in the editbox
e.click()


Thread.sleep(2000);

}

catch(Exception e){
 System.out.println("Exception - > " + e.toString());
 }
 finally{
  driver.close();
  driver.quit();
 }
} //main function ends
 
}//class ends


Above code will click the element with id attribute value is - myid

We can also click on the web element by using java script code as mentioned below.
((JavascriptExecutor) driver).executeScript("arguments[0].click()",e);


You may also like to read below topics.
  1. Press key in Selenium Webdriver in Java
  2. Read Table Data in Selenium Webdriver in Java
  3. Take the screenshot in Selenium Webdriver in Java
  4. Execute JavaScript in Selenium Webdriver in Java
  5. Handle multiple browser windows in Selenium Webdriver in Java
  6. Double-Click on the Element in Selenium Webdriver in Java
  7. Exceptions in Selenium Webdriver in Java
  8. Synchronization in Selenium Webdriver in Java

What do you think on above selenium topic. Please provide your inputs and comments. Thanks

How to enter the data in web edit box in selenium webdriver in java?

We can enter the data in editbox or input box in a webpage by using sendKeys method.

WebElement e = driver.findElement(By.id("mainb"));
e.sendKeys("hello")

Above code will enter hello in the edit box / text box having id - mainb


Complete Example to enter data in editbox using Selenium Webdriver in Java

Below example shows how we can enter the data in edit box in selenium webdriver in Java.

//import the required packages and classes.

import java.io.File;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;



//import com.sun.jna.platform.FileUtils;


@SuppressWarnings("unused")
public class OpenGoogle {
    
public static void main(String [] arg) 
{

//set the path of the chrome driver exe file

System.setProperty("webdriver.chrome.driver", "C:\\Selenuim\\chromedriver2.8.exe");

//create the new instance of the chrome browser
WebDriver driver =  new ChromeDriver();


try{

//set the implicit and page load time outs  

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS);

//navigate to given url
driver.get("https://www.google.co.in/preferences");

//Maximize the browser window
driver.manage().window().maximize();

//identify the edit box element using id
WebElement e = driver.findElement(By.id("myid"));

//enter value in the editbox
e.sendKeys("hello")


Thread.sleep(2000);

}

catch(Exception e){
 System.out.println("Exception - > " + e.toString());
 }
 finally{
  driver.close();
  driver.quit();
 }
} //main function ends
 
}//class ends
Above code will enter hello in the edit box whose id attribute value is - myid


You may also like to read below topics.
  1. Press key in Selenium Webdriver in Java
  2. Read Table Data in Selenium Webdriver in Java
  3. Take the screenshot in Selenium Webdriver in Java
  4. Execute JavaScript in Selenium Webdriver in Java
  5. Handle multiple browser windows in Selenium Webdriver in Java
  6. Double-Click on the Element in Selenium Webdriver in Java
  7. Exceptions in Selenium Webdriver in Java
  8. Synchronization in Selenium Webdriver in Java

What do you think on above selenium topic. Please provide your inputs and comments. Thanks

How to select the value from the web list in selenium web driver in java?

Example -  We can select the value from the drop down by using 3 methods.
  1. selectByVisibleText - select by the text displayed in drop down
  2. selectByIndex  - select by index of option in drop down
  3. selectByValue  - select by value of option in drop down

Consider below code. Suppose you want to select the first option in the combo box.

<select id="44">
 <option value="1">xyz</option>
 <option value="2">abc</option>
 <option value="3">pqr</option>
</select>

Complete Example in Selenium Webdriver in Java

Below Java example shows how you can select the first value from the combo box using selenium webdriver.

//import the required packages and classes.

import java.io.File;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;




@SuppressWarnings("unused")
public class OpenGoogle {
    
public static void main(String [] arg) 
{

//set the path of the chrome driver exe file

System.setProperty("webdriver.chrome.driver", "C:\\Selenuim\\chromedriver2.8.exe");

//create the new instance of the chrome browser
WebDriver driver =  new ChromeDriver();


try{

//set the implicit and page load time outs  

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS);

//navigate to given url
driver.get("https://www.google.co.in/preferences");

//Maximize the browser window
driver.manage().window().maximize();

WebElement e = driver.findElement(By.id("44"));
Select selectElement=new Select(e);

// both of the below statements will select first option in the weblist
selectElement.selectByVisibleText("xyz");  

selectElement.selectByValue("1");

Thread.sleep(2000);

}

catch(Exception e){
 System.out.println("Exception - > " + e.toString());
 }
 finally{
  driver.close();
  driver.quit();
 }
} //main function ends
 



}//class ends


You may also like to read below topics.
  1. Press key in Selenium Webdriver in Java
  2. Read Table Data in Selenium Webdriver in Java
  3. Take the screenshot in Selenium Webdriver in Java
  4. Execute JavaScript in Selenium Webdriver in Java
  5. Handle multiple browser windows in Selenium Webdriver in Java
  6. Double-Click on the Element in Selenium Webdriver in Java
  7. Exceptions in Selenium Webdriver in Java
  8. Synchronization in Selenium Webdriver in Java

What do you think on above selenium topic. Please provide your inputs and comments. Thanks

How to find the element in selenium web driver in java?

Element identification methods are common in all languages like Java, C#.Net etc with some syntactical differences.

We can identify the web elements in selenium web driver using below methods.
  1. By Xpath
  2. By Css Selectors
  3. By Id
  4. By Name
  5. By Tag Name
  6. By Class Name
  7. By Linktext
  8. By Partial Link text
Out of these methods, xpath and css selector methods are very popular and useful. We can use xpath and css selectors to identify all kinds of elements in the webpage.

We can also identify the elements using Id, Name, Class Name provided that given element is assigned some id, name or class name.

We can also identify the elements using the html tags like table, div, form etc. 

For identifying the links, selenium web driver provides 2 additional methods like link text and Partial link text.

Web Element Identification Examples in Java - 

driver.findElement(By.tagName("TD"));
Above method will return the first element with given tag - TD.

Example -

WebElement e = driver.findElement(By.tagName("div"));
Above line will get the first element whose tag is div.

To get all div elements, you can use below code
List <WebElement> we_collection = driver.findElements(By.tagName("div"));

Please note that to get the single element we use findElement method and
to get the collection of elements we use findElements method


You can also access the element if you know its class name by using below code
driver.findElement(By.className("box"));

You can access the element if you know its id by using below code
driver.findElement(By.id("selenium"));

You can also access the element if you know the value of name attribute by using below code
driver.findElement(By.name("sagar"));

To find the links in web page, there are 2 ways as mentioned below.
driver.findElement(By.partialLinkText("news"));
driver.findElement(By.linkText("hello"));

If you know the part of the name of link, you can use By.partialLinkText
If you know the whole link name, you can use By.linkText


You can also access the element if you know its xpath expression by using below code
driver.findElement(By.xpath("//table"));

You can also access the element if you know its css expression by using below code
driver.findElement(By.cssSelector("#kid"));

To find all elements you can use below syntax.

List<WebElement> cells = tr.get(1).findElements(By.tagName("td"));
Above code will select all TD elements from the webpage.

Please note that some automation engineers use different terminologies for element identification like object identification methods or Selenium locators etc. In UFT, we use object repository to store the objects. In Selenium, we do not have separate file to store the objects or elements. But we can create our custom file which may store information about objects and their locators.
What do you think on above selenium topic. Please provide your inputs and comments. Thanks

Buy Best Selenium Books

Contributors