Selenium之当鼠标悬浮时隐藏的元素才出现

在自动化过程中,有些导航按钮只有当鼠标悬浮在登录信息上时,它才能出现。这时候如果想要点击导航按钮直接用selenium的webDriver是无法定位的元素的,因为这些元素是隐藏的,只有鼠标悬浮时才出现,所以要记录一下,给大家一个参考

            Actions action = new Actions(driver);     
            WebElement nav=driver.findElement(By.xpath("/html/body/div[1]/div/div[1]/div[2]/div/ul/li/a/span"));
            //if found the link, then hover over the link to display the menu.
            if(nav.isDisplayed()){
                System.out.println("found=================");
                action.moveToElement(nav).build().perform();
            }
            
    //        action.moveToElement(nav).moveByOffset(3, 3).build().perform();
            //click the displayed menu
    //        driver.findElement(By.xpath("/html/body/div[1]/div/div[1]/div[2]/div/ul/li/ul/li[2]/a")).click();  
            //click the logout menu via js
            JavascriptExecutor js = (JavascriptExecutor) driver;
            WebElement logout=driver.findElement(By.xpath("//a[contains(@href, '/GlobalDataTaxonomy/user/signout')]"));  
    //        String myjs="$('//a[contains(@href, '/GlobalDataTaxonomy/user/signout')]').click();";
            js.executeScript("arguments[0].click();",logout);    
            driver.close();

posted @ 2015-05-26 17:55  AmySu  阅读(3997)  评论(0编辑  收藏  举报