Selenium Interview Question Part – 2

This is the second post in the series of selenium interview preparation. In this post also we are focusing on understanding basic selenium concepts. People with experience less than 3 years can expect this type of questions.

Ques 1 : What are two different methods to navigate to a particular url in a browser using Selenium WebDriver?

There are two methods to navigate to a particular url using selenium:

  • get(url)
WebDriver driver = new ChromeDriver();

public void invokeBrowser(){

String url1 = "";

String url2 = "";




Ques 2: How to send ENTER/TAB key in Selenium WebDriver?

public void searchitem(){
// Define a textbox as a Web Element with any one of the Identifier, not necessarily xPath
WebElement textbox = Driver.findElement(By.xpath("//input[@type='text']"));

//To Pass tab key

//To Pass Enter key


Ques 3: List down methods for Alert Handling?

Following are the frequently used methods within an alert.

  • Accept an alert.
  • Reject an alert.
  • Get message from an alert.
public void alertHandling(){

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

// To get message from an Alert
String message = alert.getText();
System.out.println("To get the message from an alert : "+ message);

//To accept an Alert

//To reject an Alert


Ques4: Difference type of wait statement in Selenium WebDriver?

In Selenium WebDriver, to sync up scripts there are three types of wait:

PageLoadTimeout – This is the maximum time selenium waits for a page to get load successfully on a browser. If the page takes more than this time, it will throw Timeout exception.

driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

Implicit Wait – this wait can be considered as element detection timeout. Once defined in a script, this wait will be set for all the web elements on a page.

  • Selenium keeps polling to check whether that element is available to interact with or not.
  • This is the maximum time selenium wait to interact with that web element, before throwing “Element not found exception”.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Explicit Wait – This wait can be considered as conditional wait, and is applied to a particular Web Element with a condition. There are many conditions which can be applied using explicit wait.

  • Say, for example, there is a web element on a page which takes more than expected time to appear on the page, so instead of increasing Implicit wait for a particular Web Element we can apply explicit wait to that element with a condition.
public void waitTillElementVisible(){

WebDriverWait wait = new WebDriverWait(driver, 10);



You can learn more about waits from Wait commands in Selenium web driver.

Ques5: Difference between Absolute and Relative xpath?

  • Absolute xpath starts from starting of the page. As in an html page, the first tag is HTML.
  • “/” is used to access an immediate child of the parent tag.
  • Example: html/body/table/tbody/tr[2]/td/input
  • Relative xpath starts from anywhere on the page.
  • “//” is used to access any child of the parent tag.
  • Syntax: //htmlTagname[@attribute=’value’]
  • Example: //input[@type=’text’] – It represents xpath of a WebElement which is represented by tagname input and has an attribute type = ‘text’.

To learn more about writing xpaths. Please follow Mastering Xpath article.

Ques6: Write a code to get numbers of frames on a page?

  • To get the number of frames, we will call a method called findElements which will return all the frames elements in a list, and then find its size.
public void getNumberOfFrames(){

int numberOfFrames = driver.findElements(By.tagName("iframe")).size();

System.out.println("Number of frames on a page: "+ numberOfFrames);


Ques7: How to scroll down a page using JavaScript in Selenium?

  • Scroll down operation can be performed by invoking JavaScript:
public static void scrollPage(WebDriver oBrowser, int x, int y){
String jsCommand;
JavascriptExecutor oJSEngine;

oJSEngine = (JavascriptExecutor) driver;

jsCommand = String.format("window.scrollTo(%d, %d)", x,y);


Ques8: How to get the colour of a text or a link text?

There is method element.getCssProperty(<property>) which returns the value of the css property you will pass as an argument. In case you need color property, pass the property as color.

Ques9: How to get the width of a textbox?

Ques10: How to take Screenshot in Selenium WebDriver?

  • To take a screenshot in Selenium there is a method called getScreenshotAs() from a class called TakesScreenshot.
public void takeSnapshot(String sImageFilename){
try {
TakesScreenshot camera;
File tmpFile, imageFile;

imageFile = new File(sImageFilename);

if( new File(sImageFilename).exists()){
throw new Exception("File already exists..");

camera= (TakesScreenshot) driver;

tmpFile = camera.getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(tmpFile, oImageFile);

} catch (Exception e) {

Ques11: What is the basic syntax of xpath?

Basic syntax of relative xpath:

//Html Tagname[@attribute=’value’]

Ques12: What are different methods and axes in xpath?

  • Methods  in xpath – contains(), starts-with() and text().
  • Axes in xpath – following, preceding, following-sibling, preceding-sibling, parent, ancestor are some frequently used axes in xpath.

Ques13: How to set the size of Browser window using selenium? or how to test responsiveness  of an application?

  • To set the size of a window of a Browser, method used is setSize(dim);
Dimension dim = new Dimension(500, 500);


Ques14: How to maximise a window of a Browser using selenium?

//To maximise the browser

Ques15: How to get the color of a text on a page?


Ques16: Write a code to get the status of all the checkbox on a page?

  • isSelected() – method is used to verify whether a checkbox is selected or not.
  • It’s a boolean operation returns true if selected else false.
  • To verify all the checkbox, first, get all these checkboxes (by using findElements method) in a list and then iterate this list to get the status of each checkbox.
public void getStatus(){
List<WebElement> list = Driver.findElements(By.xpath("//input[@type='checkbox']"));

for(WebElement temp : list){

Ques17: Write a code to wait for a particular element to be visible on a page?

  • Explicit wait can be used to apply conditional wait (here, condition is visibility of an element on a page)
public void waitTillElementVisible(){

WebDriverWait wait = new WebDriverWait(Driver, 90);



Ques18: Write a code to wait for a text to change its colour?

  • To apply a conditional wait on change of property(attribute) of a WebElement explicit wait can be used.
  • Here, the attribute will be color.
public void waitTillColourChange(){

WebDriverWait wait = new WebDriverWait(Driver, 90);

wait.until(ExpectedConditions.attributeContains(By.linkText("advertisement"), "color", "blue"));


Ques19: Write a code to wait for an alert to appear?

  • Waiting for an alert to appear on a page can be performed using explicit wait in Selenium WebDriver.
public void waitForAnAlert(){

WebDriverWait wait = new WebDriverWait(Driver, 90);



Ques20: How to highlight a text or an image in selenium?

  • To highlight a text or an image in Selenium, JavaScript can be injected.
public void highLightElement( ){
try {
WebElement oElement;
JavascriptExecutor oJsEngine;
String sJsCommand;
String sOldColour, sHighlightColour;

// Element to be highlighted
oElement = oDriver.findElement(By.xpath("//img[@id="add"]"));

// To get the previous color
sOldColour = oElement.getCssValue("backgroundColor");

oJsEngine = (JavascriptExecutor) oDriver;

//JavaScript command to change the color of an element to "yellow"
sJsCommand = String.format("arguments[0].style.backgroundColor=\"%s\";", "yellow");

//Executing JavaScript
oJsEngine.executeScript(sJsCommand, oElement);

// Wait for 5 seconds

//JavaScript command to change the color of an element to old color
sJsCommand = String.format("arguments[0].style.backgroundColor=\"%s\";", sOldColour);
oJsEngine.executeScript(sJsCommand, oElement);

} catch (Exception e) {

Soon we will be updating next series of Interview Questions. Till then Happy Learning 🙂

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: