Selenium Interview Questions Answers

1. 1. What is Selenium and the different components of Selenium?


Selenium is an open-source web-based functional and regression automation testing tool. Selenium automates browser. Selenium IDE, RC(Deprecated), WebDriver, and Grid are the components of the Selenium.

2. How does Selenium IDE Work?


Selenium IDE has got one of the best features called Recording and Playback. We do not have to create scripts manually. there is a recording button available in IDE. Click on the record button and whatever we execute on the firefox browser, everything will be recorded in the tool. selenium stores the attribute of the Web Element of the individual action on the web pages like ID, Name, CSS, Xpath, and when we run the script it performs an operation on the web pages accordingly

3. What is Selenese?


Selenese is the language that is generally used to write test scripts in Selenium IDE. Selenese commands are stored in the Selenium Core library which is in-build of Selenium IDE

4. What are the locators Selenium?


Locators are used to locating elements on the webpage. If you want to click, type or select any element on the webpage, we need to tell selenium to use a locator. Locators: id, name, xpath, cssSelector, linkText, partialLinkText, tagName and className

5. What is the difference between verify and assert command in Selenium IDE?


verify commands continues execution even if the text is not verified or the condition is false on the webpage. Execution will not be stopped.

Assert commands will stop execution in case of any error occurs or the condition is false. In both cases, errors will be reported in the log panel of IDE.

6. What is Webdriver?


Webdriver is an interface that implements different classes like FirefoxDriver, ChromeDriver, InternetExplorerDriver etc...It is an interface helping us to automate websites. It helps us to make our tests easier. It is a collection of open-source APIs which are used to automate the testing of websites.

7. How to find HTML elements using WebDriver?


There is a findElement method from WebElement interface which has a constructor named By class and By class have static methods id, name, xpath, cssSelector, linkText, partialLinkText etc... Example: driver.findElement(By.id("xyz11")).click();

8. How to define implicitlywait in WebDriver?


driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); Where driver is a reference of WebDriver interface. If any webpage takes a long time to load, driver will wait for a maximum of 20 seconds. We can also define time units like minutes, hours, seconds, milliseconds, microseconds, days, etc..

9. How to maximize browser window using Webdriver?


We will write below mentioned line after defining webdriver. driver.manage().window().maximize();

10. What is WebElement in Webdriver?


WebElement is an interface that accesses most commonly used methods like click, sendKeys, getText, isDiplayed, isEnabled, isSelected etc..

11. How to select a value from a drop-down list?


There is a Select class which has a WebElement as a constructor. We will pass the webelement of the dropdown list as a constructor. We can use selectByVisibleText, selectByIndex, selectByValue methods to select the specific text from the drop down list. Example: Select sel = new Select(driver.findElement(By.name("country"))); sel.selectByVisibleText("India");

12. Difference between close and quit.


Close will close the current window you are currently dealing with. Quit will close all browser windows opened by WebDriver during execution.

13. How to handle multiple elements on the webpage?


There are two approaches in order to handle multiple elements.

Approach 1:

Let's say we have a group of links which are categorized in a division or table. So first we will take WebElement of the Division where our links are grouped together. Then We will use findElements method with the locator "tagName" and that will return List. Example: WebElement catDiv = driver.findElement(By.xpath("//*[@id='columnLeft']/div[1]/div[2]")); List catLinks = catDiv.findElements(By.tagName("a"));

Approach 2:

Create custom xpaths that will point to all web elements on the page and we can directly use findElements method to store elements in a list.

14. Difference between findElement and findElements.


They both are methods of SearchContext interface. SearchContext is the super interface of the WebDriver. findElement returns WebElement and it mainly deals with the single elements. findElements returns List and it mainly deals with the multiple elements of the same group.

Example: WebElement catDiv = driver.findElement(By.xpath("//*[@id='columnLeft']/div[1]/div[2]")); List catLinks = driver.findElements(By.tagName("a"));

15. How to navigate from one page to the previous page or the next page?


To navigate from one page to the previous page: driver.navigate().back(); To navigate from one page to the next page: driver.navigate().forward(); To refresh the current page: driver.navigate().refresh();