如何在Selenium中的主菜单上选择并单击鼠标上的SubMenu 2

时间:2013-09-23 10:20:43

标签: selenium selenium-webdriver

如何在下面给出的菜单中选择并点击MainMenu鼠标上的SubMenu2?

  • 菜单
    • 子菜单1
    • SubMenu2

< / p>

我尝试使用下面给出的代码,但无法选择子菜单。请帮助解决您的线索

Actions actions = new Actions(driver);

WebElement menuHoverLink = driver.findElement(By.cssSelector("#access > ul > li > a"));
actions.moveToElement(menuHoverLink);
WebElement subLink = driver.findElement(By.cssSelector("#access > ul > li > ul > li > a"));
actions.moveToElement(subLink);
actions.click();
actions.perform();

2 个答案:

答案 0 :(得分:1)

看到您的主菜单正在使用鼠标悬停功能打开,对于子菜单,您只需要单击所需的链接,这样您就不需要在子菜单链接上执行鼠标悬停..。您可以执行以下操作:

Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.cssSelector("#access > ul > li > a"));
actions.moveToElement(menuHoverLink).build().perform();
WebElement subLink = driver.findElement(By.cssSelector("#access > ul > li > ul > li > a"));
subLink.click();

这将解决您的目的。

答案 1 :(得分:1)

我得到了解决方案:

Actions actions = new Actions(driver);
WebElement menuHoverLink =  driver.findElement(By.cssSelector("#access  > ul > li:first-child> a"));

actions.moveToElement(menuHoverLink).build().perform();

By submenucss= By.cssSelector(("#access> ul> li:first-child > ul>  li:last-child a"));

WebDriverWait wait1 = new WebDriverWait(driver, 10);
wait1.until(ExpectedConditions.presenceOfElementLocated(submenucss));
WebElement submenu= driver.findElement(submenucss);
submenu.click();