You are on page 1of 7

SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

Mouse Hover Actions in Selenium Web driver


In order to perform a 'mouse hover' action, we need to chain all of the actions that we
want to achieve in one go. So move to the element that which has sub elements and click
on the child item. It should the same way what we do normally to click on a sub menu
item.

With the actions object you should first move the menu title, and then move to the sub
menu item and click it.

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

WebElement subMenu = driver.findElement(By.cssSelector("subLinklocator"));


actions.moveToElement(subMenu);
actions.click().build().perform();
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"));

Actions action = new Actions(driver);


action.moveToElement(searchBtn).perform();

Page 1 of 7
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

WebDriver driver=new FirefoxDriver();

//Launch the application


driver.navigate().to("https://www.irctc.co.in/");
//Implicit wait condition
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

//Handling mouse hover actions


Actions actions=new Actions(driver);

//Move the mouse to the expected place(where we need to perform mouse over
action)

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

Page 2 of 7
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

Example of Handling Multiple Browser Windows in Selenium


WebDriver
Selenium WebDriver software testing tool has built in
"WebDriver.switchTo().window()" method available to switch from one window to
another window so it is very easy to handle multiple windows in webdriver. If you
remember, We can use "selectWindow" window command in selenium IDE software
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.

Page 3 of 7
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

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

public void test () throws InterruptedException

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

driver.findElement(By.xpath("//b[contains(.,'Open New Page')]")).click();

// Get and store both window handles in array

Set<String> AllWindowHandles = driver.getWindowHandles();

String window1 = (String) AllWindowHandles.toArray()[0];

System.out.print("window1 handle code = "+AllWindowHandles.toArray()[0]);

String window2 = (String) AllWindowHandles.toArray()[1];

System.out.print("\nwindow2 handle code = "+AllWindowHandles.toArray()[1]);

//Switch to window2(child window) and performing actions on it.

driver.switchTo().window(window2);

driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("My Name");

driver.findElement(By.xpath("//input[@value='Bike']")).click();

driver.findElement(By.xpath("//input[@value='Car']")).click();

driver.findElement(By.xpath("//input[@value='Boat']")).click();

Page 4 of 7
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

driver.findElement(By.xpath("//input[@value='male']")).click();

Thread.sleep(5000);

//Switch to window1(parent window) and performing actions on it.

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

//Once Again switch to window2(child window) and performing actions on it.

driver.switchTo().window(window2);

driver.findElement(By.xpath("//input[@name='fname']")).clear();

driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("Name
Changed");

Thread.sleep(5000);

driver.close();

//Once Again switch to window1(parent window) and performing actions on it.

driver.switchTo().window(window1);

Page 5 of 7
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

driver.findElement(By.xpath("//input[@value='male']")).click();

Thread.sleep(5000);

Drag and Drop using Web driver Action Class

We have taken example program to perform drag and drop. In the below example, as the
DragAndDrop divs are in a Frame, First we need to switch to the frame before performing drag
and drop. And then we also need to check for the availability
of SourceElement and DestinationElements.

Syntax for drag and drop


Actions action = new Actions(driver);
action.dragAndDrop(Sourcelocator, Destinationlocator).build().perform();

We can also make it as below:


(new Actions(driver)).dragAndDrop(element, target).perform();

We have also used Webdriver Wait Expected conditions to wait for a frame to be available and
then switch to the frame.

The below is the source image on which we will perform operation :

Page 6 of 7
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

Actions action=new Actions(driver);

driver.switchTo().frame(driver.findElement
(By.xpath("//iframe[@class='demo- frame']")));

WebElement source=driver.findElement(By.id("draggable"));
WebElement destionation=driver.findElement(By.id("droppable"));

action.dragAndDrop(source, destionation).build().perform();

Page 7 of 7

You might also like