在selenium中没有获得鼠标悬停测试脚本的预期结果

时间:2017-09-11 14:48:31

标签: java selenium action

下面是鼠标悬停在元素上的代码段。

public static void main(String[] args) throws InterruptedException
{
    System.setProperty("Webdriver.ie.driver","D://IEDriverServer.exe");
    WebDriver driver = new InternetExplorerDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.get("http://www.amity.edu/");

    String title = driver.getTitle();
    System.out.println(title);

    Actions action = new Actions(driver);
    WebElement MainTab = driver.findElement(By.xpath("//div[@class='main']/ul[2]/li[2]/a"));
    action.moveToElement(MainTab).build().perform();
}

1 个答案:

答案 0 :(得分:2)

尝试以下代码,它可以在chromefirefox浏览器中使用。

由于IE浏览器存在一些问题,因此Action类无法使用IE Browser

有关详细信息,请参阅此url

System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");   //path for your browser.
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.amity.edu/");

String page_title = driver.getTitle();
System.out.println(page_title);

WebElement main_menu = driver.findElement(By.xpath("//ul[@class='megamenu main-nav']/li/a[text()='Campuses']"));  // Get main menu
WebElement child_menu = driver.findElement(By.xpath("//ul[@class='megamenu main-nav']//li/a[text()='Chennai']")); // get Submenu called Chennai 

Actions act = new Actions(driver);
act.moveToElement(main_menu).perform();
new WebDriverWait(driver, 60).until(ExpectedConditions.visibilityOf(child_menu));     //wait for element
act.moveToElement(child_menu).click().perform();