Spread the love

Top 20 Selenium Testing Interview Questions and Answers [Updated 2020]

Are you thinking to crack Selenium automation interview?

Yes!! Great !

Please go through the these selenium webdriver interview questions and answers.

Q1 : How to count total number of rows of a table using Selenium Webdriver?
Ans:
List <WebElement> rows = driver.findElements(By.xpath(“//table[@id=’tableID’]/tr”));
int totalRow = rows.size();

Q 2: How to get element attribute using Selenium Webdriver?
Ans:
WebElement element = driver.findElement(By.id(“ElementID”));

String attributeValue =element .getAttribute(“AttributeName”) ;

Q 3: Which web driver implementation is fastest?
Ans:

HTMLUnitDriver. Simple reason is HTMLUnitDriver does not execute tests on browser

but plain http request – response which is far quick than launching a browser and executing tests.

Q 4: How do I submit a form using Selenium?
Ans:

WebElement el = driver.findElement(By.id(“ElementID”));
el.submit();

Q 5 : How to capture screen shot in Webdriver?
Ans:
File file= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File(“c:\\failedtest.png”));

Q 6 : How do I clear content of a text box in Selenium Webdriver?
Ans:
WebElement el = driver.findElement(By.id(“ElementID”));
el.clear();

Q7: Which attribute you should consider throughout the script in frame for “if no frame Id as well as no frame name”? 

Ans.

You can use…..driver.findElements(By.xpath(“//iframe”))….
This will return list of frames.
You will need to switch to each and every frame and search for locator which you want and then break the loop.

Q.8. Explain how you can login into any site if it’s showing any authentication popup for password and username? 

Ans. Pass the username and password with url
Syntax:   http://username:password@url
example:    http://john:Pass123@www.gmail.com

Q.9. Explain what are Data driven framework and Keyword driven framework? 

Ans.

Data driven framework: In this framework, the test data is separated and kept outside the Test Scripts, while test case logic resides in Test Scripts. Test data is read from the external files ( Excel Files) and are loaded into the variables inside the Test Script. Variables are used for both for input values and for verification values.

Keyword driven framework: The keyword driven frameworks requires the development of data tables and keywords, independent of the test automation. In a keyword driven test, the functionality of the application under test is documented in a table as well as step by step instructions for each test.

Q.10. What challenges have you faced while using Selenium and how you overcame?

Ans. Below are the common challenges:

  • Wrong implementation: I used the page object model  but had it implemented incorrectly. My classes were focussing on the web elements rather than they should have resembled the user actions.
  • Duplicate code: The project had many category pages. Each category had a different search function instead of handling them at a central place.
  • Less Abstraction: The framework missed on abstracting several steps into one. For example – Searching for an item took three actions such as clicking the box, typing the word, and clicking the search.
  • Ineffective use of wait: I used implicit wait with a fixed timeout. But some pages were timing out due to higher load time. I’d to adopt the Fluent wait (with a variable timeout) to overcome this problem.
  • Improper error handling: It was getting hard to debug the cause of a failed test. At some places, the {try-catch} blocks were missing and hence cases were skipping w/o giving a proper message. Therefore, I’d to refactor the code by adding asserts and exception handling.
  • Inconsistent XPath: The 90% of the objects were using XPath. And they were inconsistent as the developers kept them changed while fixing new defects. The unfortunate practice was prevailing at both ends, the Dev/QA. I’d to call a discussion with them. And finally, we came to an agreement that either to have a fixed XPath or an ID for the web elements.
  • Performance & Localization: We were using the flat files (CSV) initially to feed data to test cases. However, it had us failed in testing localization as well as beaten us on the performance. Finally, we migrated all of our test data to MySQL DB and could get fixed both these issues.
  • Monolithic tests: Earlier tests weren’t using the labeling. Honestly, there wasn’t a way to do it. Hence, we integrated our test suite with TestNG framework and got away with this limitation. Now, we have many test groups like features-based (F1, F2, F3…), priority-based (P1, P2, P3).

Q.11. What type of exceptions are available in Selenium?

Ans.

The common exceptions in Selenium are as follows.

TimeoutException: Occurs if a command doesn’t finish within the specified duration.
NoSuchElementException: Occurs if the web element with the specified attributes is not present on the page.
ElementNotVisibleException: Occurs if the element is not visible but still there inside the DOM.
StaleElementException: Occurs in the absence of an element which either got deleted or detached from the DOM.

Q.12. How will you retrieve the text present on any button of the webpage?

Ans. Here is the code for it:

WebElement element = driver.findElement(By.id("elementID"));
element.getText();

Q.13: How will you select a value from the dropdownList using selenium webdriver?

Ans. I will use Select class provided by WebDriver API.

Here is sample code for it.

Select select = new Select(driver.findElement(By.tagName("select")));
select.deselectAll();
select.selectByVisibleText("erostrum");

This will deselect all OPTIONs from the first SELECT on the page, and then select the OPTION with the displayed text of “erostrum”.

Q.14 : What all available methods are in SELECT class?

Ans.  Below are available methods:

  • selectByVisibleText
  • selectByIndex
  • selectByValue
  • getOptions

In case you want to deselect any pre-selected option, that can be done with either

  • deselectAll()
  • deselectByIndex
  • deselectByValue
  • deselectByVisibletext

Q.15 : Can you give some example of dropdownlist and write the automation code to select the dropdown values?

Ans. Sure. Let’s take below dropdownlist example.

<select id="mySelect">
<option value="option1">France</option>
<option value="option2">India</option>
<option value="option3">US</option>
</select>

Below is the automation code to select “India” from the list.

WebElement mySelectElement = driver.findElement(By.id("mySelect"));
Select dropdown= new Select(mySelectElement);
dropdown.selectByVisibleText("India");

Q.16 : How will you find if the given dropdownList supports multiple selections ?

Ans. For this , I can use isMultiple( )  method of Select classThis tells whether the SELECT element support multiple selecting options at the same time or not. It returns boolean value(true/false).