如何点击使用selenium在鼠标悬停时填充的windows菜单

时间:2012-09-20 05:09:55

标签: javascript selenium menu webdriver

在运行时加载菜单的JavaScript函数。鼠标悬停在上面时,LoadMenu函数会加载菜单项,我需要在MouseClick()事件中使用与之相关的位置。

如何使用Selenium webdriver进行此操作?

function  LoadMenus(){

        window.menu_1234567 =  new Menu("root",150,20,"Arial, Helvetica, sans-serif",12,"#336699","#333333","#ffffff","#E2E6CD","left","middle",10,0,200,-5,7,true,false,true,0,true,true);
        menu_1234567.addMenuItem("Partner Management","location='/dash-1.0/partnerconfiguration/partnerAdministration.action'");
        menu_12345678_0.addMenuItem("User Management","location='/dash-1.0/systemadminconfiguration/userSysAdmin.action'");
        menu_1234567.addMenuItem("Audit Report","location='/dash-1.0/configuration/auditReport.action'");
        menu_1234567.addMenuItem("Change Password","location='/dash-1.0/changecurrentpassword.action'");
        menu_1234567.addMenuItem("Sign Out","location='/dash-1.0/logout.action'");
        menu_1234567.hideOnMouseOut=true;
        menu_1234567.writeMenus();
     };
    LoadMenus();                

以下是HTML代码:

    <div id="menuItem0" class="menuItem" style="position: absolute; left: 0px; top: 0px; font-family: Arial,Helvetica,sans-serif; font-size: 12px; height: 20px; background-color: rgb(255, 255, 255); visibility: inherit;">
<div id="menuItemText0" class="menuItemText" style="position:absolute;left:11px;top:3px;color:#336699;">Partner Management </div>
<div id="menuItemHilite0" class="menuItemHilite" style="position: absolute; left: 11px; top: 3px; color: rgb(51, 51, 51); visibility: hidden;">Partner Management </div>
</div>

我通过selenium webdriver获得了元素“menuItemText2”,但是在element.click()方法中没有任何反应。我已尝试过下面提到的所有场景,但无法通过selenium web driver正确点击菜单项。

// case 1 tried using mouseMove
Point p=element.getLocation();
System.out.println("x="+p.getX());
System.out.println("y="+p.getY());
Robot robot = new Robot();
robot.mouseMove(p.getX(),p.getY());

// case 2 tried using moveByOffset
Actions builder = new Actions(driver); 
builder.moveToElement(element).build().perform();
builder.moveByOffset(p.x, p.y).perform();

// case 3  tried using Action builder
Actions hover = builder.moveToElement(element, p.x, p.y);
hoverOverRegistrar.perform();
hover.perform();
hover.build();
hover.click();

//case 4 tried by executing javascript and search element by name
element=    driver.findElement(By.name("Sign Out "));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("$('div#menuItemText2').mouseover();");
js.executeScript("$('div#menuItem3.menuItem').click();");

// case 5 tried by executing javascript and search element by id
element = driver.findElement(By.id("menuItem4"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("$('div#menuItem4.menuItem').hover();");
element.click();

请解释如何点击填充鼠标悬停的菜单项,并且点击事件不会与网页元素相关联。

2 个答案:

答案 0 :(得分:0)

你有没有试过Action界面?

特别是“生成行动链”这一点可以帮助你

我这样做:

private void hoverAndClick(WebElement[] hoverer, WebElement element){

    Actions actions = new Actions(this.driver);

    for (WebElement we : hoverer) {
        Action hovering = actions.moveToElement(we).build();
        hovering.perform();
    }

    element.click();
}

如果您只需要一个元素悬停,请删除数组并进行修改或复制:

private void hoverAndClick(WebElement hover, WebElement element){

    Actions actions = new Actions(this.driver);

    Action hovering = actions.moveToElement(hover).build();
    hovering.perform();
    element.click();

}

答案 1 :(得分:0)

我会修改你描述的案例5:

// case 5 tried by executing javascript and search element by id
element = driver.findElement(By.id("menuItem4"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("$('div#menuItem4.menuItem').hover();");
element.click();

以下列方式;

    String cssSelector="[id='menuItem4']";

    public void jsClick(String css){

        JavascriptExecutor js = (JavascriptExecutor) driver;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("var x = $(\'"+css+"\');");
            stringBuilder.append("x.click();");
            js.executeScript(stringBuilder.toString());
    }
    jsClick(cssSelector);

希望这能以某种方式帮助你)