Selenium Interview Questions (Part 2)
Types of wait with Syntax?
ImplicitlyWait --> Wait for a specified amount of time
We wait for 20 seconds, and after that it gives NoSuchElementException. If the element is present in 5 seconds then it should not wait for another 15 seconds.driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Explicit Wait --> Wait until the expected condition occurs if not, till the maximum specified time
This waits up to 15 seconds before throwing a TimeoutException or if it finds the element will return it in 0 – 15 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully.
WebDriverWait wait = new WebDriverWait(driver, 10);WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(>someid>)));FluentWait --> Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition.
Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as ‘NoSuchElementExceptions’ when searching for an element on the page.
Wait wait = new FluentWait(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
wait.until(ExpectedConditions.elementToBeClickable(By.id()));
Thread.Sleep(2000)
In this case, it again blindly wait for given seconds. It is not a better way because if the the element is present within this given wait time, program does not move further until the wait time finished.
How to maximize window?
driver.manage().window().maximize();How to set window to a particular dimension?
Dimentiond = new Dimention(500,600) // Width, height
driver.manage().window().setSize(d);Write/explain code...
to open an application, enter an incorrect username, pass, check the error message, enter the correct details, and verify next page opens successfully.
Driver.get(“www.gmail.com”) Driver.findElement(By.id("username-id")).sendkeys(“email @id”)
Driver.findElement(By.id("Password-id")).sendkeys(“Wrong password”)
Driver.findElement(By.id("button-id")).click()
Driver.findElement(By.xPath("errorMsg-id")).isPresent()
//Give valid details and click LoginAssert.assertTrue(“Expected Title”,Driver.getTitle())What are webdriver exceptions? Name 3 with explaination.
ElementNotVisibleException: Thrown when an element is present on the DOM, but it is not visible, and so is not able to be interacted with. Most commonly encountered when trying to click or read the text of an element that is hidden from view.
NoSuchElementException: Thrown when an element could not be found. If you encounter this exception, you may want to check the following: Check your selector used in your find_by...The element may not yet be on the screen at the time of the find operation, (the webpage is still loading) see selenium.webdriver.support.wait.WebDriverWait() for how to write a wait wrapper to wait for an element to appear.
TimeoutException: Thrown when a command does not complete in enough time.ElementNotVisibleException: Thrown to indicate that although an element is present on the DOM, it is not visible, and so is not able to be interacted with
What is StaleElementReferenceException in Selenium Webdriver and how to handle it?
Assume there is an element that is found on a web page referenced as a WebElement in WebDriver. If the DOM changes then the WebElement goes stale. If we try to interact with an element that is staled then the StaleElementReferenceException is thrown.
Cause 1: The referenced web element has been deleted completely.Assume that there is an element in a web page and the page that the web element was part of has been refreshed or the selenium script navigated away to another web page. This causes any subsequent interaction with the element to fail with the stale element reference exception.
Cause 2: The referenced element is no longer attached to the DOM
Assume a document node is removed from the DOM, its element reference will be invalidated. This causes any subsequent interaction with the element to fail with the stale element reference exception.
Solution 1: Refreshing the web page
driver.navigate().refersh();
Solution 2: Using Try Catch Block
for (int i = 0; i <= 2; i++) {
try {
driver.findElement(By.xpath("xpath here")).click();
break;
} catch (Exception e) {
Sysout(e.getMessage());
}
}Solution 3: Using ExpectedConditions.refreshed
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("table")));Use ExpectedConditions.refreshed to avoid StaleElementReferenceException and retrieve the element again. This method updates the element by redrawing it and we can access the referenced element.
wait.until(ExpectedConditions.refreshed(ExpectedConditions.stalenessOf("table")));How To Get Tooltip Text
WebElement ele = driver.findElement(By.id("age"));
// Using actions class to do mouse hover
Actions action = new Actions(driver);
action.moveToElement(ele).build().perform();
WebElement toolTipEle = driver.findElement(By.xpath("//*[@id='ui-id-118']/div"));
// Get the Tool Tip Text
String toolTipTxt = toolTipElement.getText();
/ Using assert to verify the expected and actual value
Assert.assertEquals("We ask for your age only for statistical purposes.", toolTipTxt);
Ways to find elements if not visible?
Refer earlier questions and write from tried methods