简体   繁体   中英

How to hover the mouse from left to right icons in selenium webdriver (Java)?

This is the code which I am using for moving the mouse from 'Products' to 'Find ATM Branch'(in total these are 5 web element Products, Apply Online, Payments, Ways to Bank, Find ATM Branch ):

package Advance_SeleniumPackg;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.internal.Locatable;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.support.events.internal.EventFiringMouse;

public class Mouse_Movement {

    public static void main(String[] args) throws InterruptedException {

        WebDriver webdriver=new FirefoxDriver();
        EventFiringWebDriver driver=new EventFiringWebDriver(webdriver);
        WebListener listener=new WebListener();
        driver.register(listener);

        EventFiringMouse mouse=new EventFiringMouse(driver,listener);
        driver.navigate().to("http://www.icicibank.com/");

        Thread.sleep(5000L);
        Locatable hoverItem = (Locatable) driver.findElement(By.linkText("Products"));

        Locatable hoverItem1 = (Locatable) driver.findElement(By.linkText("Find ATM Branch"));

        Coordinates MyTestCoordinates = hoverItem.getCoordinates();
        Coordinates MyTestCoordinates1 = hoverItem1.getCoordinates();


        try{
            mouse.mouseMove(MyTestCoordinates);
            mouse.mouseMove(MyTestCoordinates1);

        }catch(Throwable t){

            System.out.println(t);
        }
        Thread.sleep(3000L);

    }

}

Please use the below code to move the cursor from "Products" to "Find ATM Branch" tabs

package SamplePrograms;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class WebDriverSecondProgram {

/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    WebDriver driver = new FirefoxDriver();
    WebDriverWait wait = new WebDriverWait(driver, 100);

    driver.get("http://www.icicibank.com");
    wait.until(ExpectedConditions.titleContains("Personal Banking"));

    Actions hoverOps = new Actions(driver);
    hoverOps.moveToElement(driver.findElement(By.xpath(".//a[text()='Products']"))).build().perform();
    Thread.sleep(2000);
    hoverOps.moveToElement(driver.findElement(By.xpath(".//a[text()='Apply Online']"))).build().perform();
    Thread.sleep(2000);
    hoverOps.moveToElement(driver.findElement(By.xpath(".//a[text()='Payments']"))).build().perform();
    Thread.sleep(2000);
    hoverOps.moveToElement(driver.findElement(By.xpath(".//a[text()='Ways to Bank']"))).build().perform();
    Thread.sleep(2000);
    hoverOps.moveToElement(driver.findElement(By.xpath(".//a[text()='Find ATM Branch']"))).build().perform();
    Thread.sleep(2000);

    driver.quit();
}
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM