无法使用动作类进行鼠标悬停(movetoElement)

时间:2017-03-25 02:32:14

标签: java selenium action selenium3

AM使用以下代码并尝试在链接上进行鼠标悬停。代码正在成功执行而没有任何错误,但我无法在网页上看到任何操作。

系统规范 Windows 7的; mozilla 52.0 64 bit; 硒3.3.0

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;


public class Mouseaction {

public static void main(String[] args) {
System.setenter code hereProperty("webdriver.gecko.driver","E:\\Selenium\\geckodriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();

driver.get("http://www.flipkart.com");           
    System.out.println(driver.findElement(By.partialLinkText("Electronics")).isDisplayed());
Actions act = new Actions(driver);
    act.moveToElement(driver.findElement(By.partialLinkText("Electronics"))).build().perform();

}
}

3 个答案:

答案 0 :(得分:0)

我不确定为什么你的代码无效。我测试了下面的代码并且它可以工作。

driver.get("https://www.flipkart.com/");
WebElement link = new WebDriverWait(driver, 3)
        .until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a[title='Electronics']")));
new Actions(driver).moveToElement(link).build().perform();

答案 1 :(得分:0)

尝试使用chrome驱动程序

以下是完整的工作代码:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Mouseaction {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("http://www.flipkart.com");
        System.out.println(driver.findElement(By.xpath("//a/span[text()='Electronics']")).isDisplayed());
        Actions act = new Actions(driver);
        act.clickAndHold(driver.findElement(By.xpath("//a/span[text()='Electronics']"))).build().perform();
        Thread.sleep(12000);

        System.out.println(
                "HoverOver successfull on : " + driver.findElement(By.xpath("//a/span[text()='Electronics']")));
        driver.quit();

    }
}

控制台输出

true

HoverOver successfull on : [[ChromeDriver: chrome on XP (1e433b044b5162e96df55c46690dd943)] -> xpath: //a/span[text()='Electronics']]

答案 2 :(得分:-1)

您的定位器无法正常工作,请尝试以下操作:

    driver.manage().window().maximize();
    driver.get("http://www.flipkart.com");           

    WebElement hoverOn = driver.findElement(By.xpath("//*[@id='container']/div/header/div[2]/div/ul/li[1]/a"));
    Actions act = new Actions(driver);
    act.moveToElement(hoverOn).build().perform();