Selenium Interview Q&A 2

1)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();


 2) Give the example for method overload in WebDriver.

Frame (string), frame (int), and frame (WebElement).
 3) 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”);
4) 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.
 5) How do you simulate browser back and forward?

Driver. Navigate ().back ();

Driver. Navigate ().forward ();
6) How do you get the current page URL?

driver.getCurrentUrl ();
 7) What is the difference between ‘/’ and ‘//’?

// It is used to search in the entire structure.

/ It is used to identify the immediate child.
 8) 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.
 9) How do you achieve synchronization in WebDriver?

We can use implicit wait.

Syntax 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 with each and every line of the script automatically. It is not required to write every time. Just write it once after opening the browser.
 10) 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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s