Selenium Interview Questions and Answers
Ques 31. Write a code snippet to launch Chrome browser in WebDriver.
public class ChromeBrowserLaunchDemo { public static void main(String[] args) { //Creating a driver object referencing WebDriver interface WebDriver driver; //Setting the webdriver.chrome.driver property to its executable's location System.setProperty("webdriver.chrome.driver", "/lib/chromeDriver/chromedriver.exe"); //Instantiating driver object driver = newChromeDriver(); //Using get() method to open a webpage driver.get("http://javatpoint.com"); //Closing the browser driver.quit(); } }
Ques 32. Write a code snippet to launch Internet Explorer browser in WebDriver.
public class IEBrowserLaunchDemo { public static void main(String[] args) { //Creating a driver object referencing WebDriver interface WebDriver driver; //Setting the webdriver.ie.driver property to its executable's location System.setProperty("webdriver.ie.driver", "/lib/IEDriverServer/IEDriverServer.exe"); //Instantiating driver object driver = newInternetExplorerDriver(); //Using get() method to open a webpage driver.get("http://javatpoint.com"); //Closing the browser driver.quit(); } }
Ques 33. Write a code snippet to perform right-click an element in WebDriver.
We will use Action class to generate user event like right-click an element in WebDriver.
Actions action = newActions(driver);
WebElement element = driver.findElement(By.id("elementId"));
action.contextClick(element).perform();
Ques 34. Write a code snippet to perform mouse hover in WebDriver.
Actions action = newActions(driver);
WebElement element = driver.findElement(By.id("elementId"));
action.moveToElement(element).perform();
Ques 35. How do you perform drag and drop operation in WebDriver?
Code snippet to perform drag and drop operation:
//WebElement on which drag and drop operation needs to be performed
WebElementfromWebElement = driver.findElement(By Locator of fromWebElement);
//WebElement to which the above object is dropped
WebElementtoWebElement = driver.findElement(By Locator of toWebElement);
//Creating object of Actions class to build composite actions
Actions builder = newActions(driver);
//Building a drag and drop action
Action dragAndDrop = builder.clickAndHold(fromWebElement)
.moveToElement(toWebElement)
.release(toWebElement)
.build();
//Performing the drag and drop action
dragAndDrop.perform();
Most helpful rated by users: