Handling Mouse Events
How to handle double click?
Key Points:
- Actions class is used for advanced user interactions (mouse, keyboard).
- .doubleClick(WebElement) method simulates a double-click on the given element.
- Always end the action with .perform() to execute it.
Sample Example:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class DoubleClickExample {
public static void main(String[] args) {
// Set the path to your chromedriver
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
try {
// Navigate to the webpage
driver.get("https://example.com/double-click-demo");
// Locate the element to double click
WebElement elementToDoubleClick = driver.findElement(By.id("doubleClickBtn"));
// Create Actions object
Actions actions = new Actions(driver);
// Perform double click
actions.doubleClick(elementToDoubleClick).perform();
// Add a pause to see the result (optional)
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
} finally {
driver.quit();
}
}
}
How to handle right click?
Key Points:
- Use actions.contextClick(WebElement) to simulate a right-click on the target element.
- Use .perform() to execute the action.
- This is commonly used to test context menus or custom right-click actions.
Sample Program:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class RightClickExample {
public static void main(String[] args) {
// Set the path to your chromedriver
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
try {
// Navigate to the webpage
driver.get("https://example.com/right-click-demo");
// Locate the element to right click
WebElement elementToRightClick = driver.findElement(By.id("rightClickBtn"));
// Create Actions object
Actions actions = new Actions(driver);
// Perform right click (context click)
actions.contextClick(elementToRightClick).perform();
// Add a pause to see the result (optional)
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
} finally {
driver.quit();
}
}
}
How to drag and drop?
Key Points
- Use .dragAndDrop(source, target) for most cases.
- If drag-and-drop does not work due to JS events, try clickAndHold and moveToElement.
- Always end with .perform().
Sample Program:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class DragAndDropExample {
public static void main(String[] args) {
// Set the path to your chromedriver
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
try {
// Open a demo drag and drop site
driver.get("https://demoqa.com/droppable");
// Locate the source (draggable) and target (droppable) elements
WebElement sourceElement = driver.findElement(By.id("draggable"));
WebElement targetElement = driver.findElement(By.id("droppable"));
// Create Actions object
Actions actions = new Actions(driver);
// Perform drag and drop
actions.dragAndDrop(sourceElement, targetElement).perform();
// Optional: pause to see the result
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
} finally {
driver.quit();
}
}
}
Alternative: Click-Hold-Move-Release (Sometimes works better)
Some web apps require a more manual drag-and-drop:
actions.clickAndHold(sourceElement)
.moveToElement(targetElement)
.release()
.build()
.perform();
How to verify tooltips?
Verifying tooltips in Selenium WebDriver involves checking the text that appears when you hover over an element.
Most tooltips are shown in two ways:
- As a title attribute (default browser tooltip)
- As a custom element (shown only after hover, e.g., with JavaScript frameworks)
Here’s how to verify both types:
1. Verifying Browser Default Tooltip (title attribute)
Sample Program:
WebElement element = driver.findElement(By.id("myElementId"));
String tooltipText = element.getAttribute("title");
// Now verify tooltip text
if (tooltipText.equals("Expected tooltip text")) {
System.out.println("Tooltip verified!");
} else {
System.out.println("Tooltip does not match!");
}
Tip:
No need to hover. The tooltip text is stored in the title attribute, so you can get it directly.
2. Verifying Custom Tooltip (Appears After Hover)
Steps:
- Hover over the element to make the tooltip visible.
- Locate the tooltip element that appears.
- Get and verify the tooltip text.
Sample Program:
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TooltipVerification {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
try {
driver.get("https://example.com/tooltip-demo");
WebElement element = driver.findElement(By.id("customTooltipBtn"));
// Hover over the element to display tooltip
Actions actions = new Actions(driver);
actions.moveToElement(element).perform();
// Wait for tooltip to appear (adjust locator as per your app)
Thread.sleep(2000); // Replace with WebDriverWait for production code
WebElement tooltip = driver.findElement(By.cssSelector(".tooltip-class")); // Use the correct selector
String tooltipText = tooltip.getText();
if (tooltipText.equals("Expected tooltip text")) {
System.out.println("Tooltip verified!");
} else {
System.out.println("Tooltip does not match!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
driver.quit();
}
}
}
Tip:
For dynamic tooltips, always use WebDriverWait instead of Thread.sleep() for better reliability.
How to perform mouse hover ?
To perform mouse hover actions in Selenium WebDriver, you use the Actions class and its .moveToElement() method.
This is often used for revealing dropdown menus, tooltips, or triggering JavaScript events.
Key Points
- Use actions.moveToElement(element).perform(); to simulate mouse hovering.
- After hovering, you can interact with elements that appear (like submenus).
- For complex scenarios, chain more actions:
.moveToElement(element).click().build().perform();
Sample Program:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class MouseHoverExample {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
try {
driver.get("https://demoqa.com/menu/"); // Example site with menu on hover
// Locate the element to hover over
WebElement menuElement = driver.findElement(By.linkText("Main Item 2"));
// Create Actions object
Actions actions = new Actions(driver);
// Perform mouse hover
actions.moveToElement(menuElement).perform();
// Pause to observe the effect (optional)
Thread.sleep(2000);
} finally {
driver.quit();
}
}
}
Mouse Hover and Then Click Example
Suppose you need to hover and then click a submenu item:
Sample Program:
WebElement menu = driver.findElement(By.id("menu"));
WebElement submenu = driver.findElement(By.id("submenu"));
Actions actions = new Actions(driver);
actions.moveToElement(menu).moveToElement(submenu).click().build().perform();
Tip:
- Always use proper waits if elements are loaded dynamically!
- Let me know if you need a Python example or how to combine hover with waits.
Summary Table of Keyboard Actions
Scenario | Code Example |
Type text | element.sendKeys(“text”) |
Press ENTER/TAB/ESC | element.sendKeys(Keys.ENTER) |
Keyboard shortcuts (Ctrl+A) | actions.keyDown(Keys.CONTROL).sendKeys(“a”).keyUp(Keys.CONTROL).perform(); |
Clear field | element.clear() or element.sendKeys(Keys.CONTROL + “a”, Keys.DELETE) |
Arrow key navigation | element.sendKeys(Keys.ARROW_DOWN) |
Shift/Alt combinations | actions.keyDown(Keys.SHIFT)… |
Submit form | element.sendKeys(Keys.ENTER) or element.submit() |