How to perform mouseover events in selenium web driver using java

Automate mouseover events in selenium web driver, To perform mouseover operation in selenium web driver we will use the Actions class. Actions class in selenium.

Testcase scenario

  • Open URL “codebun.com”.
  • Perform mouseover operation on “Automation” in the menu.
  • Click on selenium web driver submenu under the menu tab.

Action class in selenium web driver with an example.

package testCases;



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;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;



public class ActionClassExamples {

public static WebDriver driver;



/*

* ................Task description............... URL= http://codebun.com

* Mouseover at Selenium in the menu. Click on katalon Studio. Selenium=>Katalon

* studio.

*/



public static void main(String[] args) {



// Set the system property.

System.setProperty("webdriver.chrome.driver", "D:\\Automation\\chromedriver_win32\\chromedriver.exe");



// Create an instance of Chrome driver.

driver = new ChromeDriver();



// Load URL.

driver.get("http://codebun.com");

WebDriverWait wait = new WebDriverWait(driver, 60);



// Wait until driver is not locate the path of "Selenium".

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='menu-item-1368']/a")));



// Get the elements is the variable to make the task easy.

WebElement seleniumTab = driver.findElement(By.xpath("//*[@id='menu-item-1368']/a"));

WebElement KatalonStudioTab = driver.findElement(By.xpath("//*[@id='menu-item-1196']/a"));



// Create an object of Actions class and pass the driver as parameter.

Actions act = new Actions(driver);



// mouse over to the seleniumTab.

act.moveToElement(seleniumTab).build().perform();



// Wait to load Katalon studio in sub menu.

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='menu-item-1196']/a")));



// Click to the Katalon studio.

act.moveToElement(KatalonStudioTab).click().build().perform();

// KatalonStudioTab.click();

driver.close();



}



}

Add a Comment

Your email address will not be published. Required fields are marked *

twenty − three =