Top 50 Selenium Webdriver Interview Questions

This blog helps you prepare for Selenium automation interview and brush up your selenium skills easily. You can find Selenium Webdriver interview questions for Experienced Professionals to beginners on this post. Selenium interview questions are divided into different topics for sake of ease.

Table of Contents

1. Selenium Webdriver Commands Interview Questions

2. Actions Class Interview Questions

3. Selenium Locators Interview Questions

4. Windows and Frame Handling Interview Questions

5. Advanced Selenium Interview Questions

6. Selenium with TestNG Interview Questions

7. OOPS, Selenium Automation Framework and Project related Interview Questions



1. Selenium Webdriver Commands Interview Questions

Q1 - What is the syntax to launch browser in Selenium Webdriver?

We can launch browser by creating an object of WebDriver interface and initialize it with some browser class like., ChromeDriver(), FirefoxDriver() etc. Please see the syntax for different browsers,

// Set Path of driver executable
String driver_executable_path = "./src/com/techlistic/utils/geckodriver.exe";
System.setProperty("webdriver.gecko.driver", driver_executable_path);
// Launch Browser - Creating a Firefox instance
WebDriver driver = new FirefoxDriver();

// For Chrome
String driver_executable_path = "./src/com/techlistic/utils/chromedriver.exe";
WebDriver driver = new ChromeDriver();

// For Safari
String driver_executable_path = "./src/com/techlistic/utils/safaridriver.exe";
WebDriver driver = new SafariDriver();

// For Opera
String driver_executable_path = "./src/com/techlistic/utils/operadriver.exe";
WebDriver driver = new OperaDriver();

// For IE
String driver_executable_path = "./src/com/techlistic/utils/iedriver.exe";
WebDriver driver = new InternetExplorerDriver();

Q2 - What is the difference between driver.get() and driver.navigate().to() commands?


Q3 - What are the different navigation commands used in Selenium Webdriver?

There are 4 navigation commands in Selenium Webdriver,

	// Navigate directly to some URL
	driver.navigate().to("https://www.techlistic.com/p/java.html");

	// Navigate Back
	driver.navigate().back();	

	// Navigate Forward
	driver.navigate().forward();

	// Refresh Page
	driver.navigate().refresh();

Q5 – What is the difference between driver.close() and driver.quit() Selenium Webdriver?

driver.close()
It is used to close the current browser window.

driver.quit()
It is used to close all the browser windows which are opened by Selenium and safely ends the session. (Destroys the webdriver instance)

Q6 - How to enter the text in a text field using Selenium Webdriver?

We can enter text in Selenium by using sendKeys() command.

driver.findElement(By.xpath("/some/xpath")).sendKeys("Some Value");

// OR

// Implementation with WebElement
WebElement FIRSTNAME =  driver.findElement(By.id("some-id"));
FIRSTNAME.sendKeys("Any Text value");

Q7 - How to check that the radio button is checked or not?

You can use isSelected() command of Selenium to see whether a radio button is checked or not.

/* isSelected()
 */

WebElement gender =  driver.findElement(By.id("some-radio-button-id"));
boolean isGenderSelected = gender.isSelected();

Q8 - How to get color or size of the font Selenium Webdriver? (how to get background color or any other css property of the web element)

We can get the font color or size or any other css property value of web element by using getCssValue() command.

/* getCssValue() Command
 */
WebElement HEADING =  driver.findElement(By.id("some-id"));

String color = HEADING.getCssValue("color");
System.out.println(color);

Q9 - How to validate any error/success message in Selenium Webdriver?

Selenium provides getText() command by using that, we can get text of the error/success message and validate it with the expected message.

String textValue = driver.findElement(By.xpath("/some/xpath")).getText();

// OR

// Implementation with WebElement
WebElement SOME_LINK =  driver.findElement(By.id("some-id"));
String linkValue = SOME_LINK.getText();
System.out.println(linkValue);

Q10 - How to select an item from the drop down menu with Selenium Webdriver? What are the different commands to select an item?

We have create the object of Select class in Selenium and then we can select an element from Select drop down menu by using any one of the following three commands:

/* Select Commands
 *  selectByIndex()
 *  selectByVisibleText()
 *  selectByValue()
 */
WebElement DROP_DOWN = driver.findElement(By.xpath("/select"));

// Create object of Select Class
Select select1 = new Select(DROP_DOWN);

// Option 1
select1.selectByIndex(2);

// Option 2
select1.selectByVisibleText("Edam");

// Option 3
select1.selectByValue("Edam");

Q11 - How to check the text is present/visible or not on page?

We can check presence of text on a page by using isDisplayed() command.

/* isDisplayed()
 */
WebElement loginButton =  driver.findElement(By.id("some-id"));
boolean isLoginDisplayed = loginButton.isDisplayed();

Q12 - How to handle Alerts or Pop-Ups using Selenium Webdriver?

Alerts or javascript pop-ups can be handled by using commands of org.openqa.selenium.Alert class in Selenium Webdriver. For details read here - Alert Handling

// Alert class object creation
// Below Code switches the control/focus of your execution to the alert
Alert alert = driver.switchTo().alert();

// Below command is used to accept the alert like click on Yes, Accept, Ok button
alert.accept();

// Below command is used to dismiss/reject the alert like click on No, Dismiss, Cancel button
alert.dismiss();

Q13 - Why we aren't recommended to use Thread.sleep() in Selenium?

Thread.sleep() stops the execution flow of the thread for particular time. Instead of that some of the Selenium wait should be used.

Q14 – What are the different wait commands in Selenium Webdriver?

There are three types of wait commands in Selenium: (Read in detail – Selenium Wait Commands):

1. Implicit Wait - Implicit wait is used to set wait time (say 30 secs) in your automation script to wait for an  element on the page before throwing exception. You can increase or decrease the wait time as per your requirement.

2. Explicit Wait – Explicit wait is also knows as conditional wait. It directs the Selenium Webdriver to wait until a condition is met.

3. Fluent Wait - Fluent wait is kind of conditional wait but with frequency.

Q15 – What is the syntax for implicit, explicit and fluent wait?

Syntax for Implicit, Explicit and Fluent wait commands:

// Implicit wait - Set wait of 10 seconds
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

// Explicit Wait
 WebDriverWait wait = new WebDriverWait(driver, 10); 
 wait.until(ExpectedConditions.alertIsPresent());

// Fluent Wait
 Wait wait = new FluentWait(WebDriver reference)
   .withTimeout(timeout, SECONDS)
   .pollingEvery(timeout, SECONDS)
   .ignoring(Exception.class);

 WebElement foo = wait.until(new Function() {
  public WebElement applyy(WebDriver driver) {
   return driver.findElement(By.id("foo"));
  }
 });

Q16 - How to handle ajax elements using Selenium Webdriver?

When we do Google search and a list of options matching to our keyword displayed on it's own without refreshing the page, is an example of ajax search. We can handle ajax elements using Explicit Wait in Selenium. Read in detail - Automating Google Search using Explicit Wait

Q17 - How to scroll vertically and horizontally using Selenium Webdriver?

We can do scroll in selenium using org.openqa.selenium.JavascriptExecutor. It is a Selenium interface which can be used to execute any javascript code snippet. We use executeScript() method of it to execute javascript code for vertical or horizontal scroll.


   // Create object of JavascriptExecutor
  JavascriptExecutor js = (JavascriptExecutor) driver;

  // Vertical Scroll
  js.executeScript("javascript:window.scrollBy(0,350)");

  // Horizontal Scroll
  js.executeScript("javascript:window.scrollBy(250,0)");

Q18 - How to take screenshot in Selenium Webdriver?

Screenshot can be taken by using org.openqa.selenium.TakesScreenshot class of Selenium.
Read in detail - Take Screenshot in Selenium


2. Actions Class Commands Interview Questions


Q1 - What is Actions class? What are its commands?

Actions class is used to handle Keyboard and Mouse Events. You need to import org.openqa.selenium.interactions.Actions in order to use Actions class.

Q2 - What are the keyboard and mouse events in Selenium Webdriver?

Q3 - How to do Mouse hover in Selenium Webdriver?

Answer - Actions Class's Mouse Hover Command

Q4 - How to do double click and right click in Selenium Webdriver?

Q5 - How to drag and drop an element in Selenium?


3. Selenium Locators Interview Questions

Q1 - What is the difference between findElement() and findElements() in Selenium Webdriver?


Q2 - How many different types of locators are in Selenium? Which one is the fastest?

Q3 – What are the different types of xpaths and what is the difference between them?


4. Windows and Frame Handling Interview Questions


Q1 How to switch to Nth browser window in Selenium Webdriver?

Case 1 -

// Create a Set and store all window handle ids in it

Set AllWindowHandles = driver.getWindowHandles(); String window1 = (String) AllWindowHandles.toArray()[0]; String window2 = (String) AllWindowHandles.toArray()[1]; // Switch to window with id 2 driver.switchTo().window(window2);
String window1 = (String) AllWindowHandles.toArray()[0];
String window2 = (String) AllWindowHandles.toArray()[1];

// Switch to window with id 2
driver.switchTo().window(window2);

Case 2 -

/* Moving Between all Windows
*/
for (String handle : driver.getWindowHandles()) { driver.
driver.switchTo().window(handle); }
}

Q2 - How to switch frames in Selenium Webdriver?

Syntax -

  // Move to frame
 driver.switchTo().frame("Target Web element")

5. Advanced Selenium Interview Questions


Q1 How to download a file using Selenium Webdriver?

Selenium can't directly interact with window dialogues but we can interact with them by keyboard interactions using Selenium's Keys class or Java's Robot class.

You can download a file using Robot class of Java.

/* Download file by clicking on Save button of window dialog
 */
 
 // Create object of Robot class
 Robot r = new Robot();  
 
 // Press Alt and S key, it will shift the focus to 'Save' button of Windows dialog
 r.keyPress(KeyEvent.VK_ALT);
 r.keyPress(KeyEvent.VK_S);
 
 // Now, release S and Alt key
 r.keyRelease(KeyEvent.VK_S);
 r.keyRelease(KeyEvent.VK_ALT);
 
 // Press Enter Key as Focus is on Save button, so it will be 
 r.keyPress(KeyEvent.VK_ENTER);
 
 // Release Enter key
 r.keyRelease(KeyEvent.VK_ENTER);

Q2 - How to upload a file with Selenium Webdriver?

Selenium can't directly interact with window dialogues but we can interact with them by keyboard interactions using Selenium's Keys class or Java's Robot class.

Q3 - How to find broken links of a web page using Selenium Webdriver?

Q4 - How to Handle Multiple Browser Tabs Using Selenium Webdriver?

Multiple browser tabs can be handled using Keys enumerator and switchTo() method. 
Read in detail- Multiple Browser Tabs Handling 


Q5 - How to Achieve Code Reuseablity & Maintainablility in Selenium Code?

Code re-usability can be achieved by creating page methods (action methods) and use those methods in your test method code instead of direct selenium commands.

Q6 - How to Handle Dynamic Web Table in Selenium WebDriver?

We can handle dynamic web table by preparing dynamic xpath of table at runtime. 
Read in detail - Handle Dynamic Table

Q7 - How to Extract Table Data/Read Table Data Using Selenium WebDriver?

We can extract table data by preparing dynamic xpath of table at runtime. 
Read in detail - Extract Table Data

Q8 - How to Take Partial Screenshot in Selenium Webdriver?

Partial screenshot can be taken by using org.openqa.selenium.Point class of Selenium. We can get height and width of the element by using Point class. 
Read in detail - Take Partial Screenshot


6. Selenium with TestNG Interview Questions


Q1 - What is TestNG? How it can be integrated with Selenium?


Q2 - How to create setup and teardown methods in TestNG?


Q3 - What is the difference between @BeforeClass, @BeforeMethod and @AfterClass, @AfterMethod in TestNG?


Q4 How to set priority of test methods in TestNG?



7. OOPS, Selenium Automation Framework and Project related Interview Questions


Q1 - How to implement oops concepts  in selenium?


Q2 - What are the different types of automation frameworks?

There are different types of automation frameworks, but most commonly used are,

Linear Framework
Keyword Driven Framework
Modular Framework
Data Driven Framework
Page Object Model
Hybrid Framework


Q3 - What automation framework do you use in your project and what are the important components of that framework?


Q4 - How many test cases do you automate daily?


Q5 - How you can automate CAPTCHA?

No, we can't automate CAPTCHA. In fact captcha is placed to restrict the automation of that page.


Q6 - How your framework is integrated with your CI/CD pipeline? And which tool are you using for CI/CD?

There are different CI/CD tools present in the market like., Jenkins, Babmoo, GitLab etc. You can explain about your project's tool.


Good luck for your interview!


Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.

Comments

Popular posts from this blog

10 Best Demo Websites for Selenium Automation Testing Practice

Automate an e-Commerce website with Selenium Webdriver

Automate Browser Actions and Menu links with Selenium Webdriver

Top 25 Selenium Webdriver Commands for Test Automation

Automate Google Search with Selenium Webdriver