Selenium Webdriver interview questions and answers

Are you preparing for an Automation interview? then you must have basic knowledge about java and Selenium webdriver. Selenium web driver contains many methods to perform automation. I am going to share some important Interview questions and answer for the selenium web driver.

In Selenium webdriver, which command takes you forward by one page on the browser’s history?

a) navigate.forward()

b) Navigate.forward()

c) navigate().forward()

d) Navigate.forward

e) navigate_forward()

In Seleniun webdriver, which of the following commands retrieves the text of HTML element?

a) selectText()

b) getText()

c) getElementText()

d) getText(WebElement)

Which of the following is the correct ‘Webdriver’ command to “delete all cookies in the browser”?

a) Driver.deleteAllCookies()

b) Driver.manage().deleteCookies()

c) Driver.manage().deleteAllCookies()

d) Driver.deleteCookies()

 

In webdriver, which methods navigate to a URL?

a) goToUrl(“url”)

b) navigate().to(“url”)

c) getUrl(“url”)

d) get(“url”)

If you want to validate that a button has appeared on a page, which two commands would be the best two to use?

(A) verifyTextPresent/assertTextPresent
(B) verifyElementPresent/assertElementPresent
(C) verifyAlertPresent/assertAlertPresent
(D) verifyAlert/assertAlert

How to select a value from a dropdown in Selenium WebDriver?

Select class in WebDriver is used for selecting and deselecting options in a dropdown. The objects of the Select type can be initialized by passing the dropdown web element as a parameter to its constructor.

WebElement testDrop = driver.findElement(By.id("testingDropdown"));

Select dropdown = new Select(testDrop);

WebDriver offers three ways to select from a dropdown:

selectByIndex: Selection based on index starting from 0

dropdown.selectByIndex(5);

selectByValue: Selection based on value

dropdown.selectByValue(“Books”);

selectByVisibleText: Selection of option that displays text matching the given argument

dropdown.selectByVisibleText(“The Alchemist”);

In Selenium webdriver, selectAllOptions() is a valid command.

a) True

b) False

Which of the following is used to toggle a checkbox in a web form?

a) Element.toggle()

b) Element.click()

c) Element.set()

d) Element.select()

Which of the following Selenium API enables running Selenium 1.0 tests in web driver?

a) Selenium RC

b) None of the listed options

c) Webdriver

d) WebDriverBackedSelenium

Which of the following will terminate the test when the check fails

a) Verify

b) Assert

What is the difference between the “/” and “//” in the X path?

Single Slash “/” – The Single slash is used to create Xpath with absolute path i.e. the XPath would be created to start selection from the document node/start node.

Double Slash “//” – The Double slash is used to create Xpath with a relative path i.e. the XPath would be created to start selection from anywhere within the document.

If you wanted to find the sibling input that is after input in the DOM, what would the XPath look like?

Ans: //input/following-sibling::input

What is meant by Selenese?

Selenium commands, also known as “Selenese” are the set of commands used in Selenium that run your tests. For example, command – open (URL); launches the desired URL in the specified browser and accepts both relative and absolute URLs. A sequence of Selenium commands (Selenese) together is known as a test script.

If an element got added after the page has loaded what command would you use to make sure the test passed in the future?

(A) waitForElementPresent
(B) pause
(C) assertElementPresent

What is the most common way to find an element on a page?

(A) Id
(B) Xpath
(C) CSS Selector
(D) Name

Pick two from the following if you wanted to do a partial match on an attribute on an element from the beginning of the value:

(A) contains()
(B) starts-with()
(C) ends-with()
(D) A & B

What is the best call for finding multiple elements using XPath?

(A) findElementByXpath
(B) findElementsByXPath
(C) findElementByCssSelector
(D) Both B & C

In Selenium webdriver, which of the following is a valid select statement that selects a value from a dropdown element?

a) All of the listed options

b) selectByIndex()

c) selectByValue()

d) selectByVisibleText()

Will a findElements type call throw a NoSuchElementException when it can’t find the element?

Ans: No, it will not throw an exception. It will return an empty list.

What versions of Internet Explorer does WebDriver support?

Ans: All versions of IE6, IE7, IE8, and IE9 for both 32-bit and 64-bit installations

Which of the following expression is used for “anything”?

(A) **
(B) .*
(C) *.
(D) *+

Which of the following is not a wait command in Selenium?

(A) waitForActive
(B) waitForAlert
(C) waitForTitle
(D) None of these

Which one is a class in Selenium?

(A) WebDriverWait
(B) WebElement
(C) WebDriver
(D) getPageSource

Selenium command for entering text into text boxes?

(A) sendKeys()
(B) sendKey()
(C) sendKey
(D) SendsKeys()

Which of the following language is not supported by Selenium

(A) PHP
(B) C#
(C) ASP
(D) Java

What is the best element locator in selenium webdriver?

(A) ID
(B) NAME
(C) xPATH
(D) CLASS

Which of the following method is used to work with multiple browser windows?

(A) getMultipleWindows()
(B) getWindowHandles()
(C) getWindowhandle()
(D) Both B &C

Selenium method to get content that is inside any HTML tags?

(A) getText()
(B) getValue()
(C) get()
(D) getAttribute()

Select the odd one used in Selenium

(A) Pattern Matching
(B) XPath
(C) Id
(D) CSS selector

Key Points about Selenium Web driver!!

Load a web page in a current browser window using selenium

Loading a web page in the current browser window Here is a list of possible ways to load a web page

driver.get(java.lang.String)
driver.navigate().to(java.lang.String)
driver.navigate().to(java.net.URL)

Web Page Back or forward methods in selenium Java

Navigation() below is the list of possible ways to navigate back or forward in selenium

Driver.Navigate().Back()
Driver.Navigate().Forward()
Refresh page
refresh() is the only method to refresh the page in selenium

Driver.Navigate().Refresh()

Selenium method to get the page title in java

driver.getTitle()

Current URL Current url can be obtained using method getCurrentUrl()

driver.getCurrentUrl()

Page Source Whole page source be collected using getPageSource() method

driver.getPageSource()

Locating web elements

driver.findElement(org.openqa.selenium.By)
– 0 match -> throws exception
– 1 match -> returns a WebElement instance
– 2+ matches -> returns only the first match from web page
driver.findElements(org.openqa.selenium.By)
– 0 match -> returns an empty list
– 1 match -> returns a list with one WebElement
– 2+ matches -> returns list with all matching WebElements

Inspecting elements in web browsers

Firefox
– Firebug add-on (Right click -> Inspect element / F12)
– Firefinder add-on (Try out your CSS & Xpath expressions)
Chrome
– Built-in (Right click -> Inspect element / F12)
IE
– Built-in (Tools -> Developer Tools / F12)

Finding element by id

driver.findElement(By.id(”some_id”));
– Ideal solution, however…
– Ids don’t always exist
– Their uniqueness is not enforced
– Used by developers as well

Finding element by Class Name

driver.findElement(By.className(”some_class_name”));

Finding Element by Linktext

driver.findElement(By.linkText(”Sign in”));

driver.findElement(By.partiallinkText(”Sign”))

Finding name by Name

driver.findElement(By.name(”password”));
<input id=”modCustLoginPassword” name=”password”>

Find element by tagName

Email address
driver.findElement(By.tagName(”label”));

Find elment by Support Classes

Return all that matches each of the locators in sequence

driver.findElements(new ByChained(by1, by2))

Return all that matches any of the locators in sequence

driver.findElements(new ByAll(by1, by2))

Find element by CSS Selector in selenium

Absolute path

driver.findElement(By.cssSelector(”html>body>div>p>input”));

Relative path

driver.findElement(By.cssSelector(”input”));

Attribute selection

driver.findElement(By.cssSelector(”button[name]”));

driver.findElement(By.cssSelector(”button[name=‚cancel’]”));

driver.findElement(By.cssSelector(”img:not[alt]”));

Id selection

driver.findElement(By.cssSelector(”#save”));

Class selection

driver.findElement(By.cssSelector(”.login”));

Combined selection

driver.findElement(By.cssSelector(”button#save”));

driver.findElement(By.cssSelector(”input.login”));

First matching child of the specified tag

driver.findElement(By.cssSelector(”div#students:first-child”));

Nth matching child of the specified tag

driver.findElement(By.cssSelector(”#loginForm:nth-child(3)”));

First matching enabled tag

driver.findElement(By.cssSelector(”button:enabled”));

best way to finding element by XPath in selenium

Absolute path

driver.findElement(By.xpath(”html/body/p/input”));

Relative path

driver.findElement(By.xpath(”//input”));

Attribute selection

driver.findElement(By.xpath(”//input[@id=’username’]”));
driver.findElement(By.xpath(”//*[@id=’myId’]”))

Selenium Commands to handle dropdown select

select[ByIndex, ByVisibleText, ByValue]
deselect[ByIndex, ByVisibleText, ByValue]
deselectAll()

Selenium methods to get page size in java

driver.manage().window().getSize().getHeight();
driver.manage().window().getSize().getWidth();
driver.manage().window().setSize(Dimension d);
driver.manage().window().maximize();

Selenium methods to get Position in java

driver.manage().window().getPosition()..getX();
driver.manage().window().getPosition()..getY();
driver.manage().window().setPosition(Point p);

Selenium method to Handle windows in java

String windowHandle = driver.getWindowHandle();
Iterator windowIterator = browser.getWindowHandles();

driver.switchTo().window(windowHandle);