11. Handle Basic Dropdown in Selenium WebDriver?

In order to handle Drop Down, we make use of the object of Select Class which can handle dropdown.
First locate the dropdown box element, then store the address in a WebElement  type variable and then pass the address into Select class as Value.
Example:
WebElement ele  = driver.findElement(By.xpath(“xpathsss”));
Select sel = new Select(ele);
sel.selectByVissibleText(“Bangalore Job Seekers”);
sel.selectByIndex(3);
sel.selectByValue(“BJS”);
System.out.println(sel.isMultiple);


12. How to perform Drag and Drop in Selenium?

We need to store the From and To WebElements on which we need to perform drag and drop. Then using the object of Actions class, pass driver as a parameter on to it and perform the drag and drop Action. Using the Action class, we can click and hold the from Element , move to the To Element and release it to the To element.
Example:
WebElement from = driver.findElement(By.xpath(“xpathofthefromelement”));
WebElement to = driver.findElement(By.xpath(“xpathofthetoelement”));
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(from).moveToElement(to).release(to).build();
dragAndDrop.perform();


13. How to perform Advance activity in Selenium like- Mouse Hover, RightClick, DoubleClick, Keyboard Event?

We make use of the Actions class. The Actions class provided by Selenium Webdriver is used to generate complex user gestures including right click, double click, drag and drop etc.

DoubleClick: Here, we are instantiating an object of Actions class. After that, we pass the WebElement to be double clicked as parameter to the doubleClick() method present in the Actions class. Then, we call the perform() method to perform the generated action.
Actions act = new Actions(driver);
WebElement elem = driver.findElement(By.id(“elementid”));
action.doubleClick(elem).perform();

Mouse Hover:
Actions act = new Actions(driver);
WebElement ele = driver.findElement(By.xpath(“elementxpaths”));

act.moveToElement(ele).build().perform();

RightClick: For Right Click on a particular link or any web-element, Selenium Webdriver has contextClick() methods available in Actions class.
1. contextClick():: Right click on a page.
2. contextClick(WebElement):: Right click on a particular WebElement.
Actions act = new Actions(driver);
act.contextClick(driver.findElement(By.linktext(“BJS”))).perform();

Keyboard Event:
Actions act = new Actions(driver);
act.contextClick(driver.findElement(By.xpath(“xpxpxpxhhh”))).sendKeys(keys.ARROW_DOWN).sendKeys(keys.ARROW_DOWN).sendKeys(keys.ENTER).build().perform();


14. What is Implicit wait, Explicit Wait and Fluent Wait in Selenium Webdriver?

Implicit Wait: An implicit wait is to tell WebDriver that we would like it to wait for a certain amount of time before throwing an exception that it cannot find the element on the page.
Example:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Explicit Wait: The explicit wait is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or the maximum time exceeded before throwing an “ElementNotVisibleException” exception.
Example:
WebDriverWait  wait= new WebDriverWait(driver, 30);
WebElement elem = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“xpathsssss”)));

Fluent Wait: It is just a plain class & using this class we can wait until specific conditions are met.
Example:
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);

WebElement element= wait.until(new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver)
{
return driver.findElement(By.id(“sampleid”));
}
}


15. How to handle frames in Selenium Webdriver?

Frames are kind of html document which are embedded on other html documents. It can be <iframe> or <frameset>.
If iFrame is there, there must be a frame id. IF Frameset, then count the number of frames, and then select a frame by its name or ID or index, etc.
driver.switchTo().frame(Integer);
driver.switchTo().frame(“String”);
driver.switchTo().frame(“WebElement”);
driver.switchTo().frame();


16. How to handle Alert in Selenium Webdriver?

We got a class called as Alert class to handle Java Alerts. Since it is not a Selenium Driver object, we need to switch to the Alert type driver. We make the use of switchTo() method to switch.
Example:
Alert ale = driver.switchTo().alert();
ale.accept(); //(positive,yes,ok,etc).
ale.dismiss();  //(Negative, No, Cancel, etc).


17. How to Maximize and Minimize in WebDriver?

Maximizing: driver.manage().window().maximize();
Minimizing: Unfortunately Selenium WebDriver doesn’t provide any native support for minimizing a browser. If we don’t want to see the browser in action during automation than we can use Headless browsers like HTMLUnitDriver, PhantomJS etc.


18. How to take Screenshots in Selenium? Write the code snippet?

For taking screenshots Selenium has provided TakesScreenShot interface. In this interface you can use getScreenshotAs method which will capture the entire screenshot in form of file then using FileUtils we can copy screenshots from one location to another location.
Example:
//Taking Screenshot and storing as a FILE format.
File src = ((TakeScreenshot)driver).getScreenshotAs(OutputType.FILE);
//Copy the Screenshot to desired location using copyfile method
FileUtils.copyFile(src, new File(“c:/selenium/error.png”));


19. How to capture Tooltip in Selenium Webdriver?

By using Actions class object we can use moveToElement to perform Actions.
Example:
//Create Actions class Object and pass driver.
Actions act = new Actions(driver);
//Store the tooltip xpath in a WebElement.
WebElement tooltipelement = driver.findElement(By.xpath(“”));
//Mouse Hover to the tooltip message
act.moveToElement(tooltipelement).perform();
//Extract the text
System.out.println(tooltipelement.getText());


20. How to scroll page in Selenium Webdriver?

We need to create an object of JavascriptExecuter, & with the help of it we can executeScript for the element to ScrollIntoView.
ScrollIntoView:
Example:
//create instance of Javascript Executer of Selenium.
JavascriptExecuter je = (JavascriptExecuter) driver;
//Identify the WebElement which will appear after scrolling down
WebElement ele = driver.findElement(By.xpath(“xxxxxxx”)));
//execute query which will actually scroll until that eleent is not appeared on page.
je.executeScript(“arguments[0].scrollIntoView(true)”, element);


One response to “Selenium Interview Questions for Freshers and Experienced”

Leave a Reply