我尝试了下面的代码。

public class FindingMultipleElements {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.navigate().to("http://automationpractice.com/index.php");
        Actions act = new Actions(driver);
        WebElement women = driver.findElement(By.xpath("//*[@id='block_top_menu']/ul/li[1]/a"));
        //women.click();
        Point p1 = women.getLocation();
        int x = p1.getX();
        int y = p1.getY();
        System.out.println("X:"+x+" Y:"+y);
        act.moveByOffset(x, y).click(driver.findElement(By.linkText("T-shirts"))).build().perform();

    }
}


java - 鼠标悬停操作在演示网站的Selenium Webdriver中不起作用-LMLPHP

我需要点击女性类别中的“ T恤”链接。使用鼠标悬停操作无法单击链接。

最佳答案

使用以下代码:

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.navigate().to("http://automationpractice.com/index.php");
 driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

WebElement women = driver.findElement(By.cssSelector("ul>li:nth-child(1)>a[title='Women']"));

Actions builder = new Actions(driver);
builder.moveToElement(women).perform();//this will hover to women
Thread.sleep(1000);//avoid using this type of wait. wait using until.

driver.findElement(By.cssSelector("ul>li:nth-child(1)>a[title='T-shirts']")).click();//this will click on t-shirt


希望这会帮助你。

08-07 05:34