Selenium Interview Questions and Answers Set 2

11. How do perform drag and drop using WebDriver?

Use Action class

Actions act = new Actions (driver);
WebElement source = driver.findElement (By.xpath (“”)); //source ele which you want to drag
WebElement target = driver.findElement (By.xpath (“”)); //target where you want to drop
act.dragAndDrop(source, target).perform();

12. Give the example for method overload in WebDriver.

Frame (string), frame (int), and frame (WebElement).

13. How do you upload a file?

To upload a file we can use sendKeys() method.

driver.findElement (By.xpath (“input field”)).sendKeys (“path of the file which u want to upload”);

14. How do you click on a menu item in a drop down menu?

If that menu has been created by using select tag then we can use the methods selectByValue () or selectByIndex () or selectByVisibleText (). These are the methods of the Select class.

If the menu has not been created by using the select tag then we can simply find the xpath of that element and click on that to select.

15. How do you simulate browser back and forward?

Driver. Navigate ().back ();
Driver. Navigate ().forward ();

Weekend / Weekday Batch

16. How do you get the current page URL?

driver.getCurrentUrl ();

17. What is the difference between ‘/’ and ‘//’?

// It is used to search in the entire structure.
/ It is used to identify the immediate child.

18. What is the difference between find Element and find Elements?

Both methods are abstract method of WebDriver interface and used to find the WebElement in a web page. Find Element () – it used to find the one web element. It return only one WebElement type.
FindElements () it used to find more than one web element. It returns List of WebElement.

19. How do you achieve synchronization in WebDriver?

We can use implicit wait.

driver.manage ().timeouts ().implicitly Wait (10, TimeUnit.SECONDS);

Here it will wait for 10sec if while execution driver did not find the element in the page immediately. This code will attach to each and every line of the script automatically. It is not required to write every time. Just write it once after opening the browser.

20. 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”);
String value = s.getRow (rowNum).getCell (cellNum).getStringCellValue (); // read data
s.getRow (rowNum).getCell (cellNum).setCellValue (“value to be set”); //write data
FileOutputStream FOS = new FileOutputStream (“path of file”);
wb.write (FOS); //save file