0% found this document useful (0 votes)
2 views

selenium08_12

The document provides several Java code examples demonstrating how to handle browser tabs, mouse actions, and interactions using Selenium WebDriver. It includes scripts for closing tabs, mouse hovering over dropdown menus, right-clicking to open context menus, and performing drag-and-drop actions. Additionally, it explains the use of the Actions class for various mouse operations in Selenium.

Uploaded by

sai vivek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

selenium08_12

The document provides several Java code examples demonstrating how to handle browser tabs, mouse actions, and interactions using Selenium WebDriver. It includes scripts for closing tabs, mouse hovering over dropdown menus, right-clicking to open context menus, and performing drag-and-drop actions. Additionally, it explains the use of the Actions class for various mouse operations in Selenium.

Uploaded by

sai vivek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

(I’ll do it later)

12/08/2021
Write a script to close the Child tab 1st then the Parent tab by using Iterator.

public class HandlingTabs


{
static
{
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
}

public static void main(String[] args)


{
WebDriver driver = new ChromeDriver();
driver.get("https://demo.actitime.com/login.do");
driver.findElement(By.linkText("actiTIME Inc.")).click();
Set<String> allTabwh = driver.getWindowHandles();
Iterator<String> itr = allTabwh.iterator();
String pwh = itr.next();
String cwh = itr.next();
driver.switchTo().window(cwh);
driver.close();
driver.switchTo().window(pwh);
driver.close();
}

HANDLING MOUSE ACTIONS


By using Mouse we can perform the following actions:-
1) Mouse Hover (Handling drop down menu)
2) Right Click (Context Click)
3) Drag and Drop
4) Double Click

How do you handle Drop Down Menu / Mouse Hover in Selenium?


Ans) Mouse Hover means moving the mouse pointer to a particular location. Drop Down
Menu means it is an element on which if we move the mouse pointer, it will display
the list of options. To handle drop down/ mouse hover we use moveToElement() of
Actions class.

Action is an Interface and Actions is a class.


Actions class is present in org.openqa.selenium.interactions package.

Actions class is mainly used for Mouse Actions and it has parametrized constructor
where it takes WebDriver as an argument.
Whenever we call any methods of Actions class, we have to use perform() at the last.

All the methods present of Actions class is an example for Method Overloading.

AUTOMATE the following scenario:-


1) Open the browsers
2) Go to vtiger.com
3) Mouse hover to ‘Resource’ Tab
4) Click on Contact us in the drop down menu
5) Get the Bengaluru, India Phone No. And print it on the console.
6) Close the Browser.

public class MouseHandlingActions1


{
static
{
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
}
public static void main(String[] args)
{
WebDriver driver = new ChromeDriver();
driver.get("https://www.vtiger.com/");
driver.manage().window().maximize();
WebElement resourceTab =
driver.findElement(By.partialLinkText("Resources"));
Actions a = new Actions(driver); //passing obj ref var of type WebDriver
in Actions class
a.moveToElement(resourceTab).perform(); //perform() - execute
driver.findElement(By.partialLinkText("Contact Us")).click();
String phnno =
driver.findElement(By.xpath("//p[contains(text(),'Bengaluru')]/../p[2]")).getText();
System.out.println(phnno);
driver.close();
}
}

How do you Handle Context Click (Context Menu)?


Right clicking in mouse is called as Context Click.
When we right click on any element we get list of options which is called as Context
Menu.
To right click on any element we use contextClick() of Actions class.
To select the required options in the Context Menu we press the Shortcut Keys such as
T- New Tab, W- New Window.

Write a Script to open Actitime link in new window.

public class RightClick


{
static
{
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
}
public static void main(String[] args) throws AWTException, InterruptedException
{
WebDriver driver = new ChromeDriver();
driver.get("https://demo.actitime.com/");
WebElement link = driver.findElement(By.linkText("actiTIME Inc."));
Actions a = new Actions(driver);
a.contextClick(link).perform();
Robot r = new Robot();
r.keyPress(KeyEvent.VK_W);
Thread.sleep(5000);
driver.quit();

}
How we perform drag and drop in Selenium.
By using drapandDrop() of Actions class.

Example-

URL:- http://www.dhtmlgoodies.com/submitted-scripts/i-google-like-drag-drop/index.html

Program:-

public class HandlingDragandDrop {


static
{
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
}
public static void main(String[] args) throws AWTException, InterruptedException
{
WebDriver driver = new ChromeDriver();
driver.get("http://www.dhtmlgoodies.com/submitted-scripts/i-google-like-
drag-drop/index.html");
WebElement src = driver.findElement(By.xpath("//h1[text()='Block 1']"));
WebElement dest = driver.findElement(By.xpath("//h1[text()='Block 4']"));
Actions a = new Actions(driver);
a.dragAndDrop(src, dest).perform();

How do you perform Double click?


By using doubleclick() present in Actions class.

You might also like