无法使用鼠标悬停访问子菜单链接

时间:2014-10-14 13:22:59

标签: selenium selenium-webdriver

我正在尝试访问子菜单链接“什么是Flex?”来自

的“关于Flex”

http://flex.apache.org/使用selenium Web Driver。

每当执行鼠标悬停时,子菜单会突出显示并在我可以访问子菜单之前消失。

*导入了一些不必要的包。请不要介意。 代码:

package com.ram.workingtitle;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;  
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.Alert;
import org.openqa.selenium.interactions.Actions;
import static org.testng.Assert.assertTrue;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.*;

public class Mouseover {
    public static void main(String[] args) throws Exception
    {
        WebDriver driver= new FirefoxDriver();
        driver.navigate().to("http://flex.apache.org/");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);        

        /*
        Used Firepath for generating xpath
        */
        WebElement element=driver.findElement(By.xpath(".//*[@id='nav']/li[2]/a"));
        WebElement sub=driver.findElement(By.xpath(".//*[@id='nav']/li[2]/ul/li[1]/a"));

        Actions action=new Actions(driver);
        action.moveToElement(element).build().perform();

        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        action.moveToElement(sub).build().perform();

        System.out.println("executed");
    }
}

请帮忙。

感谢和安培;此致 拉姆。

1 个答案:

答案 0 :(得分:0)

   Actions action = new Actions(webDriver); 
            action.moveToElement(webDriver.findElement(By.xpath("//*[@id='nav']/li[2]/a")))
            .build()
            .perform();
            Thread.sleep(5000);
            webDriver.findElement(By.xpath("//*[@id='nav']/li[2]/ul/li[1]/a")).click();

注意:Thread.sleep不是一个好习惯。只需在程序或/和WebDriverWait中使用隐式等待。

相关问题