Mouse hover action in Selenium Webdriver

Mouse hover action in Selenium Webdriver,While working with web application menus you will face a problem that mouse hover,once mouse hover then only remaining menus will be displayed.In this post you will learn Mouse hover action in selenium webdriver using Web elements ,Actions classes in Selenium Web Driver.Below are the way of doing this is by using Action in class.

Mouse hover action in Selenium WebDriver


Mouse hover action in Selenium Webdriver

Method 1:

Here directly using link Text to check mouse hover for main menu or sub menu to identify the elements in webpage.

WebElement element = driver.findElement(By.linkText("More.."));
Actions action = new Actions(driver);
action.moveToElement(element).build().perform();
driver.findElement(By.linkText("SQL")).click();

Some times it will not work then we can use another method as below to select SQL link from More.. menu according to above menu.

Also Read

Datadriven Framework using selenium
Verify element is enable in Selenium
Read Data From properties file using Selenium
Launch Firefox Browser using GeckoDriver
Selenium-Testng examples to execute multiple classes
Selenium WebDriver Methods
Generate HTML Reports using Selenium

For some applications mouse over will work upto certain fraction of seconds at that situation we can go for below method easily to identify the elements.This method will work deffinately with the help of action.moveToElement(element).click().build.perform

package com.gmail.account;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.apache.tools.ant.taskdefs.Exit;
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

public class MouseOver{

WebDriver driver;
String baseUrl;

@Test
public void mouseHover(){

driver = new FirefoxDriver();
String baseUrl = "http://www.wikishown.com";
driver.get(baseUrl);
driver.manage().window().maximize();
//Use Action
Actions action = new Actions(driver);
//Mouse over on elements using WebElments
WebElement element = driver.findElement(By.linkText("More.."));
//Now mouse should move to particular element
action.moveToElement(element).click().build().perform();
Thread.sleep(2000);
//Identifying the SubMenu and click on Subbmenu
action = new Actions(driver);
WebElement subelement=driver.findElement(By.linkText("SQL"));
//Thread.sleep(5000);
action.moveToElement(subelement).click().build().perform();

}

}

Run above script as Right Click on Script - Run As  - TestNG Test.

Conclusion:

I am expecting method 2 is very useful in your projects,in case it is working please provide your valuable comments and suggestions and please feel free to contact me through comment.Thank you for reading.

Post a Comment

0 Comments