Selenium Interview Questions And Answers

1. What is Page Object Model(POM)? What are its advantages?

Page Object Model is a design pattern for creating an object repository for web UI elements. Each web page in the application is required to have its own corresponding page class. The page class is thus responsible for finding the WebElements in that page and then perform operations on those web elements.
The advantages of using POM are:

– Allow us to separate operations and flows in the UI from verification – improves code readability

– Since the Object Repository is independent of test cases, multiple tests can use the same object repository

– Reusability of code

2. Explain the different exceptions in Selenium?

The most common exceptions in selenium are:

– TimeoutException: The exception is thrown when a command performing an operation does not complete in stipulated time.

– NoSuchElementException: This exception is thrown when an element with given attributes is not found on the web page.

– ElementNotVisibleException: This exception is thrown when the element is present in the DOM, but not visible on the page.

– StaleElementException: This exception is thrown when either the element is deleted from DOM or no longer attached to it.

3. How do you locate an element by partially comparing its attributes in XPath?

XPath supports the contains() method. It allows partial matching of attribute’s value. It helps when the the attribute uses dynamic value while also having some fixed part.
See the below example –

//*[contains(@id,'java')]

 

4. How do you locate elements based on the text in XPath?

We can call the text() method. The below expression will get the elements that have text nodes that equal ‘Java’

//*[text()='Java']

5. What is the selenium command to delete session cookies?

webdriver.manage.deleteAllCookies();

 

6. What is the difference between GetWindowHandle() and GetWindowHandles() methods?

webdriver.getWindowHandle() – It gets the handle of the active web page

webdriver.getWindowHandles() – It gets the list of handles of all the pages open at that time

7. How to handle Web based alerts/pop-ups in Selenium?

Webdriver exposes the following methods to handle such popups:

  • Dismiss(): It handles the alert by simulating the cancel button
  • Accept(): It handles the alert by simulating the Ok button
  • GetText(): You may call it to find the text shown by the alert
  • SendKeys(): This method simulated keystrokes in the alert window
8. Write code to wait for a particular element to be visible on a page.
WebDriverWait wait = new WebDriverWait(driver,20); 
Element = wait.until(ExpectedConditions.visibilityofElementLocated(By.xpath("")));
9. How to scroll down a page using JavaScript in Selenium?
JavaScriptExecutor js = (JavaScriptExecutor)driver; 
js.executeScript("windows.scrollBy(0,500)");
10. How to scroll down to a particular element in Selenium?
JavaScriptExecutor js = (JavaScriptExecutor)driver; 
js.executeScript("arguments[0].scrollIntoView();",element);

 

11. How can you fetch an attribute value from an element in Selenium?

We can fetch the attribute value of an element by using the getAttribute() method. Sample code:

WebElement eLogin = driver.findElement(By.name("Login"));
String LoginClassName = elogin.getAttribute("classname");

 

12. How to retrieve typed text from a textbox?

We can use getText() method to retrieve some text from textbox. Sample code:

WebElement eLogin = driver.findElement(By.name("Login")); 
String loginText = eLogin.getText();

 

13. How to take screenshots in Selenium Webdriver?

File scrFile = ((TakeScreenshot)driver).getScreenshotAs(ouputType.FILE);

 

14. Can we enter text without using SendKeys() method?

We can do it using JavaScriptExecutor.

JavaScriptExecutor js = (JavaScriptExecutor)driver; 
js.executeScript("document.getElementById('Login').value=Enter text");

15. How to select a value in a dropdown?

We can select value using Select class. Sample code:

WebElement myElement = driver.findElement(By.name("dropdown")); 
Select select = new Select(myElement); 
select.selectByVisibleText(Text); 
select.selectByIndex(Index); 
select.selectByValue(Value);

 

16. What is fluent wait in Selenium?

A fluent wait is a type of wait in which we can also specify polling intervals(intervals after which driver will try to find the element) along with the maximum timeout value.

Wait wait = new FluentWait(driver);
 .withTimeout(20, SECONDS);
 .pollingEvery(5, SECONDS);
 .ignoring(NoSuchElementException.class); 
 WebElement textBox = wait.until(new Function(){ 
 public WebElement apply(WebDriver driver){ 
 return driver.findElement(By.Id("textBoxId")); }});

 

17. How to locate a link using its text in Selenium?

Using linkText() and partialLinkText() we can a locate a link. The difference between the two is linkText matches the complete string passed as parameter to the link texts. Whereas partialLinkText matches the string parameter partially with the link texts.

WebElement link1 = driver.findElement(By.linkText("artOfTesting")); 
WebElement link2 = driver.findElement(By.linkText("artOf"));

 

18. How to do drag and drop in selenium?

Using Actions class, drag and drop can be performed in Selenium. Sample code:

Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(SourceElement)       
 .moveToElement(TargetElement) 
 .release(TargetElement)
 .build(); 
dragAndDrop.perform();

 

19. How to handle alerts in Selenium?

In order to accept or dismiss an alert box the alert class is used. This requires first switching to the alert box and than using accept() or dismiss()  command.

Alert alert = driver.switchTo().alert();
alert.accept();
alert.dismiss();

20. How to launch a batch file in Selenium Webdriver?

We usually run a batch file or executable file for setting up the prerequisites before starting the automation. You can use the below java code for this purpose:

Process batch = Runtime.getRuntime.exec("path of batch file");
batch.waitFor();

 

21. What is the process to start Chrome/IE browser?

If you want to start a browser then, just set the system properties as mentioned below:

// For the IE web browser 
System.setProperty("webdriver.ie.driver,"iedriver.exe");
WebDriver driver = new InternetExplorerDriver(); 
//For the Chrome web browser 
System.setProperty("webdriver.chrome.driver,"chromedriver.exe file path");
WebDriver driver = new ChromeDriver();

 

22. How would you simulate the right click operation on WebDriver?

You can make use of the Actions class features.

Actions action = new Actions(driver); 
action.moveToElement(element).perform(); 
action.contextClick().perform();

 

23. Write the code for reading and writing to excel through Selenium?

FileInputStream fis = new FileInputStream("path of excel file"); 
Workbook wb = WorkbookFactory.create(fis); 
Sheet s = wb.getSheet("sheetName");
// read data
String value = s.getRow(rowNum).getCell(cellNum).getStringCellValue();
// write data 
s.getRow(rowNum).getCell(cellNum).setStringCell("value to be set"); 
FileOutputStream fos = new FileOutputStream("path of file"); 
wb.write(fos); //save file

 

24. How can you check that element is not present using Selenium?

If the below method returns false than we can assert that element is not present in DOM.

public boolean isElementPresent(By locatorKey) { 
 try { driver.findElement(locatorKey); 
 return true; } 
catch (org.openqa.selenium.NoSuchElementException e) { return false; } }

 

25. What is Page Factory and how it is implemented in Selenium?

Page Factory class in Selenium is an extension to the Page Object Design pattern. It is used to initialize the elements of the page object or instantiate the page objects itself.

Annotations in Page Factory are like this:

@FindBy(id = “userName”) 
WebElement txt_UserName;

OR

@FindBy(how = How.ID, using = “userName”)
WebElement txt_UserName;

We need to initialize the page object like this:

PageFactory.initElements(driver, Login.class);

@CacheLookUp – If we know that element is always present on the page, it is best to use this to save some time:

@FindBy(id = "userName") 
@CacheLookup 
private WebElement txt_UserName;

 

WATCH OUR YOUTUBE VIDEO PLAYLIST ON SELENIUM:

 

Share

Bijan Patel

Founder & Creator of QAScript | 12+ years of IT Experience | Full Stack Automation Engineer | Blogger | Trainer

You may also like...

2 Responses

  1. Gwyn Lige says:

    First time visiting your website, I like it!

  2. Bookmarked!, I enjoy your site!

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!