如何在Selenium 2.0中的鼠标悬停动态菜单项上执行鼠标单击

时间:2013-03-06 07:23:16

标签: java selenium selenium-webdriver

我正在使用Selenium WebDriver开发Walmart自动化。我已经编写了一个功能,将鼠标悬停在Departments菜单“Home,Furniture& Patio”上,以便突出显示,我可以单击“Appliances”链接。这是我写的函数但它似乎没有悬停在元素上。

    public void NavigateDepartments(){
        WebElement ApplianceLink = driver.findElement(By.xpath("//*[div='Home, Furniture & Patio']"));
    Actions myMouse = new Actions(driver);
        myMouse.moveToElement(ApplianceLink).build().perform();
    ApplianceLink.click();

}

我也试过给出绝对路径 Xpath(“/ html / body / div / div / div [3] / div / div / div / ul / li [3] / div / div”)找到元素,它也不起作用。我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

您应首先在主菜单上悬停,然后转到新元素

WebElement menu = driver.findElement(By.xpath("//path to *appliance*"));
WebElement parentMenu = driver.findElement(By.xpath("//*[div='Home, Furniture & Patio']"));
Actions builder = new Actions(driver);
builder.moveToElement(parentMenu).moveToElement(menu).click().build().perform();