-1

AM 使用下面的代码并尝试将鼠标悬停在链接上。代码成功执行,没有任何错误,但我在网页上看不到任何操作。

系统规格: Windows 7;Mozilla 52.0 64 位;硒 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();

}
}
4

3 回答 3

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']]
于 2017-03-27T08:48:41.293 回答
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();
于 2017-03-27T03:47:45.820 回答
-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();
于 2017-03-25T04:41:16.843 回答