Top 51 Most Important Selenium WebDriver Interview Questions

This Selenium Interview questions post will definitely help you prepare for a Selenium automation interview and brush up your Selenium skills easily.

You can find Selenium interview questions and answers and also Selenium Java interview questions from Experienced to Beginners in this post. Selenium interview questions are divided into different topics for the sake of ease.

Table of Contents

1. Selenium Basic 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 Automation Framework Questions

7. Java OOPS Interview Questions

8. Top 21 Selenium TestNG Interview Questions

10. Top 10 Interview Preparation Websites



1. Selenium Basic Interview Questions


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

We can launch the 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?

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

ii. 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 whether 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 the background color or size of the font with Selenium WebDriver?

We can get the font color or size or any other CSS property value of the 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 the 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 created the object of the 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 whether the text is present/visible or not on the page?

We can check the 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

// And 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() pauses the execution flow of the thread for a particular time, which is not the ideal way to wait. 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 a wait time (say 30 secs) in your automation script to wait for an element on the page before throwing an exception. You can increase or decrease the wait time as per your requirement.

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

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


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

The 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 apply(WebDriver driver) {
   return driver.findElement(By.id("foo"));
  }
 });



Q16 - How to handle Ajax elements using Selenium WebDriver?

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

In the following code, we are typing the keyword "selenium tutorial techlistic" in the Google search box. And then the suggestion box appears and we are handling that Ajax suggestion box with the WebDriverWait class's presenceOfElementLocated() function.


//enter techlistic tutorials in search box

driver.findElement(By.name("q")).sendKeys("selenium tutorial techlistic");

//wait for suggestions
WebDriverWait wait=new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.className("sbtc")));

WebElement list=driver.findElement(By.className("sbtc"));
List rows=list.findElements(By.tagName("li"));

    for(WebElement elem:rows) {
	System.out.println(elem.getText());
    }
}



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

We can do a scroll in Selenium using org.openqa.selenium.JavascriptExecutor. It is a Selenium interface that 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 screenshots in Selenium WebDriver?

The screenshot can be taken by using org.openqa.selenium.TakesScreenshot class of Selenium. You can read here in detail - Take Screenshot in Selenium

In the following code, we are creating an object of the File class of Java and storing the screenshot taken using the TakesScreenshot's getScreenShotAs() method. And then store it with some name using the FileUtils class's copyFile() method of Java.


// Create File object and save screenshot of current webpage inside it
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

// Copy screenshot file to a location with some name and extension you want
FileUtils.copyFile(screenshot, new File("D:\\screenshot.jpg"));



Q19- What are the advantages of using Selenium WebDriver?

  • Selenium is open source.
  • It has huge support from the community, which means if you are stuck at some point while working on it, you can get answers for your problem from the internet easily as a lot of people are working on it.
  • It supports multiple languages like., Java, Python, C#, Ruby, Perl, etc.
  • It supports almost every browser., like Firefox, chrome, edge, safari, etc.
  • Regular updates.
  • Easy to maintain the code.
  • It reduces the time and effort with respect to Manual testing.


Q20- What are the disadvantages of using Selenium WebDriver?

  • We can only test web applications with Selenium and cannot automate Mobile or Desktop applications.
  • No reporting mechanism is available. We have to integrate third-party tools like TestNG or Allure for reporting.
  • No in-built logging mechanism.
  • One should have basic knowledge of HTML to locate elements.
  • One should have proper knowledge of any programming language.


Q21- What is Selenese?

Selenese is the set of Selenium commands. We can divide Selenese or Selenium commands into three categories:
  • Actions - They are used to perform interactions with web elements.
  • Accessors - They are used to store values in variables.
  • Assertions - They are used for validation.

Q22- Can you name some of the Selenium exceptions?

  • TimeoutException
  • WebDriverException
  • NoAlertPresentException
  • NoSuchWindowException
  • NoSuchElementException


Q23- What are Desired Capabilities in Selenium WebDriver?

DesiredCapabilities is a class in Selenium that is used to set/get browser properties or you can also say it is used for setting the configuration of the browser before running Selenium tests on it. You can set accept SSL errors, enable/disable alerts, set browser name or version, etc.


Q24- Can we perform Database testing with Selenium?

Selenium itself doesn't support database testing, but we can perform database testing using any language's database library. Like if we are using Selenium with Java then we can use Java's JDBC library to write out database tests. JDBC allows us to execute the SQL queries and we can verify the SQL query results.


Q25- Can we handle window dialogs using Selenium?

No window cannot interact with window dialogs on its own. That's why upload file dialogs or download file dialogs can't be handled with Selenium. 

But we can handle them in the Selenium script by using third-party tools or some other Java libraries. Like., we can make use of the Robot Class library of Java to handle those dialogs or we can use third-party tool AutoIT to handle such window dialogs. We can integrate AutoIT code inside the Selenium WebDriver code.


Q26 - What different types of testing can be done using Selenium?

Selenium is basically used for Functional testing and it doesn't support non-functional testing like Performance testing, UI testing, Usability testing, or Security testing. And in functional testing, it is mostly used for automating
  • Regression Testing
  • Smoke Testing
  • Sanity Testing


2. Actions Class Interview Questions

Q27 - What is the 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 the Actions class. 
This class includes keyboard and mouse actions such as double click, right-click, drag & drop, mouse hover, and clicking multiple elements.


Q28 - What are the keyboard and mouse events in Selenium WebDriver?

We can perform the following Keyboard and Mouse events using the Actions class:

i. Keyboard events: You can press any key of the keyboard, 
  • Key Up
  • Key Down
  • sendKeys()
ii. Mouse events:
  • click()
  • doubleClick()
  • contextClick()
  • clickAndHold()
  • dragAndDrop()
  • moveToElement()
  • moveByOffset(x, y)
  • release()


Q29 - How to do Mouse hover in Selenium WebDriver?

We can perform Mouse Hover using the Actions class in Selenium WebDriver. Let's take a look at the code to do so, ( Read in detail - Actions Class Mouse Hover Command )


/* Mouse Hover
 */

// Xpath for Menu
WebElement Menu_Link = driver.findElement(By.xpath("/html/some/xpath"));

// Xpath for Sub Menu
WebElement SubMenu_Link = driver.findElement(By.xpath("/html/some/xpath"));

// Create object of Actions class
Actions actions = new Actions(driver);

// Move cursor to Menu link (Mouse hover on menu link so that sub menu is displayed)
actions.moveToElement(Menu_Link);

// Click on Submenu link (whcih is displayed after mouse hovering curson on menu link)
actions.click(TSHIRTS_Submenu_Link).build().perform();
 


Q30 - How do double-click and right-click in Selenium WebDriver?

i. Double Click - We can perform double click by using the Action class's doubleClick() method.


/* Double Click
 */
Actions actions = new Actions(driver);
WebElement PRODUCT_CATEGORY = driver.findElement(By.xpath("/html/some/xpath"));
actions.doubleClick(PRODUCT_CATEGORY);
actions.build().perform();



ii. Right Click - We can perform right-click by using the Action class's contextClick() method.


/* Right Click
 */

// Locate web element
WebElement PRODUCT_CATEGORY = driver.findElement(By.xpath("/html/some/xpath"));

// Create object of actions class
Actions actions = new Actions(driver);

// Right click on PRODUCT_CATEGORY element
actions.contextClick(PRODUCT_CATEGORY);
actions.build().perform();



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

We can perform drag and drop using Action classes.


/*   Drag And Drop
 */

// Weblements for source and target
WebElement source = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));

// Create object of actions class
Actions action = new Actions(driver);
action.dragAndDrop(source, target);
action.build().perform();



3. Selenium Locators Interview Questions


Q32 - What is the difference between findElement() and findElements() in Selenium WebDriver?


i. findElement() - It is used to locate a single element on the web page. It returns a single WebElement.

ii. findElements() - It is used to get all the web elements having the same element locator. For e.g., if we would use driver.findElements(By.id("text")), then it will return a list of all the web elements present on the web page having id "text".


Q33 - How many different types of locators are in Selenium?

There are 8 different types of locators that can be used in Selenium to find an element. (Read in detail - Selenium Locators Tutorial)
  1. ID
  2. Name
  3. Link Text
  4. Partial Link Text
  5. Tag Name
  6. Class Name
  7. CSS Selector
  8. XPath


Q34 - Which one is the fastest and slowest locator?

Generally, ID is considered the fastest, and xpath is considered as the slowest locator. But when it comes to choosing a locator for your automation, we should pick the most reliable one which is of course ID.
 

Q35 - What are the different types of xpaths and what is the difference between them?

XPATH is also known as Extensible Markup Language Path. It's a language by using which we can query XML documents and can locate elements in Selenium. There are two types of xpaths, 

i. Absolute xpath - It starts with single slash '/', and it starts from the first tag which is html tag. Ex., /hml/body/div[3]/input

ii. Relative xpath - It starts with double slash '//', relative path can be created from anywhere in the web page. Ex., //div[@id='name']/div/span/input


4. Windows and Frame Handling Interview Questions


Q36 How to switch to the 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); }
}


Q37 - How to switch frames in Selenium WebDriver?

Switch frames can be done using switchTo() method.
  // Move to frame
 driver.switchTo().frame("Target Web element")


5. Advanced Selenium Interview Questions


Q38 How to download a file using Selenium WebDriver?

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

You can download a file using the Robot class of Java.

/* Download the file by clicking on the Save button of the 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);


Q39 - How to upload a file with Selenium WebDriver?

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


Q40 - How to find broken links of a web page using Selenium WebDriver?


Q41 - How to Handle Multiple Browser Tabs Using Selenium WebDriver?

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


Q42 - How to Achieve Code Re-useability & Maintainability in Selenium Code?

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


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

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


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

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


Q45 - How to Take Partial Screenshots in Selenium WebDriver?

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


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


Q46 - How to implement Oops concepts in selenium?

You can explain your existing framework and how OOPS concepts are implemented in it.


Q47 - What are the different types of automation frameworks?

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

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



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

You can answer this with regard to your automation framework.


Q49 - How many test cases do you automate daily?

Generally, this answer depends on the complexity of the feature that you are automating. But if the feature is not much complex then you can automate 2-3 test cases a day.


Q50 - How you can automate CAPTCHA?

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



Q51 - 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., Jenkins, Bamboo, GitLab, etc. You can explain your project's tool. 


Good luck with your interview!


Author
Passionately writing and working in Tech Space for more than a decade.

    Comments

    1. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

      Selenium Certification Training in Electronic City

      ReplyDelete
    2. Thank you for sharing such a useful article. I had a great time. This article was fantastic to read. Continue to publish more articles on
      Cracking The Coding Interview
      data structures and algorithms

      ReplyDelete
    3. Thanks for publishing such great information. You are doing such a great job. This information is very helpful for everyone. Keep sharing about Website Development Service Provider. Thanks.

      ReplyDelete
    4. I sincerely believe that a day will come when every big decision and life changing events will originate from our sucessful use of Internet. etvbharat com

      ReplyDelete
    5. The cost of an Insurance Policy for your family would be about $12,000. What Is The Cost Of Vyvanse Without Insurance

      ReplyDelete

    6. Please keep sharing more and more wonderful articles.

      ReplyDelete

    Post a Comment

    Popular posts from this blog

    Top 7 Web Development Trends in the Market

    What is Java Class and Object?

    17 Best Demo Websites for Automation Testing Practice

    Top Mobile Test Automation Tools for Efficient App Testing: Features and Cons

    Mastering Selenium WebDriver: 25+ Essential Commands for Effective Web Testing

    Selenium IDE Tutorial: How to Automate Web Testing with Easy-to-Use Interface

    What Role Graphic Design Services Play in Marketing

    Automation Practice: Automate Amazon like E-Commerce Website with Selenium

    What's New in Selenium-Automated Testing