Below are six examples of how you can use Selenium WebDriver to move the mouse to a specific point on a webpage using Java. I will provide detailed explanations for each code snippet along with the expected output.

Example 1:

             java
Actions actions = new Actions(driver);
actions.moveByOffset(100, 200).build().perform();

             

Explanation:

– Here, we create an `Actions` object named `actions` with the WebDriver instance `driver`.

– The `moveByOffset` method moves the mouse relative to its current position. In this case, it moves the mouse by 100 pixels to the right (X) and 200 pixels down (Y).

– The `build().perform()` method is used to perform the actions.

Expected output:

The mouse cursor should move to the coordinates 100 pixels to the right and 200 pixels down from its current position.

Example 2:

             java
WebElement element = driver.findElement(By.id("elementId"));
Actions actions = new Actions(driver);
actions.moveToElement(element, 20, 30).build().perform();

             

Explanation:

– We first find the web element on the webpage with the given `elementId` using `driver.findElement()`.

– We create an `Actions` object named `actions`.

– The `moveToElement` method moves the mouse to the specified element with an offset of 20 pixels to the right (X) and 30 pixels down (Y).

– The `build().perform()` method is used to perform the actions.

Expected output:

The mouse cursor should move to the specified web element with an offset of 20 pixels to the right and 30 pixels down from its top-left corner.

Example 3:

             java
Actions actions = new Actions(driver);
actions.moveByOffset(200, 0).build().perform();

             

Explanation:

– This example is similar to Example 1 but moves the mouse horizontally by 200 pixels to the right.

Expected output:

The mouse cursor should move 200 pixels to the right from its current position on the webpage.

Example 4:

             java
WebElement element = driver.findElement(By.id("elementId"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

             

Explanation:

– We find the web element with the specified `elementId` using `driver.findElement()`.

– We cast the WebDriver instance to `JavascriptExecutor` to execute JavaScript code.

– The `scrollIntoView(true)` method is called on the element to scroll it into view.

Expected output:

The webpage should be scrolled so that the specified element is in view.

Example 5:

             java
Actions actions = new Actions(driver);
actions.moveByOffset(-50, -50).build().perform();

             

Explanation:

– This example moves the mouse cursor 50 pixels to the left and 50 pixels up from its current position.

Expected output:

The mouse cursor should move 50 pixels to the left and 50 pixels up on the webpage.

Example 6:

             java
WebElement element = driver.findElement(By.id("elementId"));
Locatable hoverItem = (Locatable) element;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseMove(hoverItem.getCoordinates());

             

Explanation:

– We find the web element with the specified `elementId` using `driver.findElement()`.

– We cast the element to `Locatable` and get the `Mouse` instance using `getMouse()`.

– We move the mouse cursor to the coordinates of the specified element using `mouseMove()`.

Expected output:

The mouse cursor should move to the top-left corner of the specified web element.

By running these examples with appropriate modifications based on your webpage and element locations, you should be able to move the mouse cursor to specific points on the webpage using Selenium WebDriver in Java.

Was this article helpful?
YesNo

Similar Posts