Handling Browser Based Alerts and Pop Ups Mouse Hover Actions in Selenium Web Driver
Handling Browser Based Alerts and Pop Ups Mouse Hover Actions in Selenium Web Driver
SELENIUM WEBDRIVER
There are the four methods that we would be using along with the Alert interface.
1) void dismiss() The dismiss() method clicks on the Cancel button as soon as the
pop up window appears.
2) void accept() The accept() method clicks on the Ok button as soon as the pop up
window appears.
3) String getText() The getText() method returns the text displayed on the alert box.
4) void sendKeys(String stringToSend) The sendKeys() method enters the specified
string pattern into the alert box.
Copy the below code in note pad and save it as .Html file, click on the Try It button alert
will generate
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display a confirm box.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
confirm("Press a button!");
}
</script>
</body>
</html>
Page 2 of 9
First we need to create new action builder instance by passing the webdriver instance,
then.
Below is the sample code to perform Mouse hover action
Example 1:
Actions actions = new Actions(driver);
WebElement mainMenu = driver.findElement(By.linkText("menulink"));
actions.moveToElement(mainMenu);
Here 'build()' method is used to compile all the list of actions into a single step and
ready to be performed.
Example 2:
Actions action = new Actions(driver);
WebElement mainMenu = driver.findElement(By.linkText("MainMenu"));
action.moveToElement(mainMenu).moveToElement(driver.findElement(By.xpath("submenuxpath"))).clic
k().build().perform();
There are cases where you may just want to mouse hover on particular element and
check if the button state/color is changing after mouse hover.
Below is the example to perform mouse hover
WebElement searchBtn = driver.findElement(By.id("searchbtn"));
Page 4 of 9
actions.moveToElement(driver.findElement(By.id("irctc_tourism"))).perform();
//Perform mouse over operation and click on the element
actions.moveToElement(driver.findElement(By.linkText("Air Packages")));
actions.click().build().perform();
testing tool to select another window. If window do not have any title or both window
has same title then it was very difficult to select other window in selenium IDE software
testing tool. WebDriver software testing tool has made it very easy. You can handle
multiple windows even if all the windows do not have any title or contains same title.
If you wants to work with multiple tabs then viewTHIS POST and wants to work with
multiple IFrames then view THIS POST.
WebDriver.getWindowHandles()
In WebDriver software testing tool, We can use "WebDriver.getWindowHandles()" to
get the handles of all opened windows by webdriver and then we can use that window
handle to switch from from one window to another window. Example Syntax for getting
window handles is as bellow.
Set<String> AllWindowHandles = driver.getWindowHandles();
WebDriver.switchTo().window()
WebDriver.switchTo().window() method is useful to switch from one window to another
window of software web application. Example syntax is as bellow.
driver.switchTo().window(window2);
Bellow given webdriver example of switching window will explain you it deeply. Execute
it in your eclipse and try to understand how webdriver do it.
Copy bellow given @Test method part of handling multiple windows of webdriver and
replace it with the @Test method part of example given on THIS PAGE.(Note : @Test
method is marked with pink color in that linked page).
@Test
Page 6 of 9
driver.switchTo().window(window1);
driver.findElement(By.xpath("//option[@id='country6']")).click();
driver.findElement(By.xpath("//input[@value='female']")).click();
driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click();
driver.switchTo().alert().accept();
Thread.sleep(5000);
Page 8 of 9
Page 9 of 9