我正在使用 Selenium WebDriver 进行沃尔玛自动化工作。我编写了一个函数,将鼠标悬停在部门菜单“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") 查找该元素,但它也不起作用。我错过了什么吗?
最佳答案
您应该首先将鼠标悬停在主菜单上,然后移至新元素
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();
关于java - 如何在 Selenium 2.0 中的动态菜单项的鼠标悬停上执行鼠标单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15241327/