Professional Documents
Culture Documents
SELENIUM WEBDRIVER
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.
Example 1:
Actions actions = new Actions(driver);
WebElement mainMenu = driver.findElement(By.linkText("menulink"));
actions.moveToElement(mainMenu);
Page 1 of 7
SUDHEER REDDY ANKIREDDYGARI
SELENIUM WEBDRIVER
//Move the mouse to the expected place(where we need to perform mouse over
action)
actions.moveToElement(driver.findElement(By.id("irctc_tourism"))).perform();
actions.moveToElement(driver.findElement(By.linkText("Air Packages")));
actions.click().build().perform();
Page 2 of 7
SUDHEER REDDY ANKIREDDYGARI
SELENIUM WEBDRIVER
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.
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
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
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);
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);
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();
driver.switchTo().window(window1);
Page 5 of 7
SUDHEER REDDY ANKIREDDYGARI
SELENIUM WEBDRIVER
driver.findElement(By.xpath("//input[@value='male']")).click();
Thread.sleep(5000);
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.
We have also used Webdriver Wait Expected conditions to wait for a frame to be available and
then switch to the frame.
Page 6 of 7
SUDHEER REDDY ANKIREDDYGARI
SELENIUM WEBDRIVER
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