Selenium Interview Questions

Ques. What is the fundamental difference between XPath and CSS selectors?

Ans. The fundamental difference between XPath and CSS selector is – using XPaths we can traverse up in the document i.e. we can move to parent elements. Whereas using the CSS selector, we can only move downwards in the document.

ArtOfTesting

Selenium Interview Questions

Ques. What are the different ways to refresh a browser?

Ans.   Using driver.navigate().refresh() command.  Using sendKeys(Keys.F5) on any textbox on the webpage.  Using driver.get(“URL”) on the current URL or using driver.getCurrentUrl()  Using driver.navigate().to(“URL”) on the current URL or 

ArtOfTesting

driver.navigate().to(driver.getCurrentUrl());

Selenium Interview Questions

Ques. What is the difference between driver.findElement() and driver.findElements() commands?

Ans. findElement() returns a single WebElement (found first) based on the locator passed as a parameter. Whereas findElements() returns a list of WebElements, all satisfying the locator value passed.

ArtOfTesting

Another difference between the two is if no element is found then findElement() throws NoSuchElementException whereas findElements() returns a list of 0 elements.

Selenium Interview Questions

Ques. What is the difference between driver.getWindowHandle() and driver.getWindowHandles() in Selenium?

Ans. The driver.getWindowHandle() returns a handle of the current window (a single unique identifier). Whereas driver.getWindowHandles() returns a set of handles of all the windows available.

ArtOfTesting

Selenium Interview Questions

Ques. What is a relative XPath?

Ans. A relative XPath is a way of locating an element using an XML expression, starting from anywhere in the HTML document. In this way, there are different ways of creating robust relative XPaths that are unaffected by changes in other UI elements. Example - //input[@id='username']

ArtOfTesting

Selenium Interview Questions

Ques. Can we move back and forward in the browser using Selenium?

Ans. Yes, using driver.navigate().back() and driver.navigate().forward() commands, we can move backward and forward in a browser.

ArtOfTesting

Selenium Interview Questions

Ques. Write the code to double-click an element.

Ans.  Actions action = new Actions(driver); WebElement element=driver.findElement(By.id("elementId")); action.doubleClick(element).perform();

ArtOfTesting

Selenium Interview Questions

Ques. What are DesiredCapabilities in Selenium WebDriver?

Ans.  Desired capabilities are a set of key-value pairs that are used for storing or configuring browser-specific properties. For example - browser's version, platform, etc in the browser instances.

ArtOfTesting

Selenium Interview Questions

Ques. How can we find all the links on a web page?

Ans. All the links are of anchor tag 'a'. So by locating elements of tagName 'a' we can find all the links on a webpage. List<WebElement> links = driver.findElements(By.tagName("a"));

ArtOfTesting

Selenium Interview Questions

Ques. What is Page Object Model or POM?

Ans. Page Object Model(POM) is a design pattern in Selenium. A design pattern is a solution or a set of standards that are used for solving commonly occurring software problems.

ArtOfTesting

Now coming to POM – POM helps to create a framework for maintaining selenium scripts. In POM for each page of the application, a class is created having the web elements belonging to the page and methods handling the events on that page. The test scripts are maintained in separate files and the methods of the page object files are called from the test scripts file.