1

我想使用 selenium web 驱动程序单击以下菜单中的“CookBooks”。它出现在鼠标悬停,它是一个 javascript 菜单。请帮我 。以下是该菜单的 html 视图。

<li>
<a href="#">
<span>Set Up</span>
</a>
    <ul>
    <li>
        <a onclick="validateLevelOfOwnership('MaintainCookbook.html');return false;" href="javascript:void(0)">
        <span>Cookbooks</span>
        </a>
    </li>
    <li>
        <a onclick="validateLevelOfOwnership('MaintainCategories.html');return false;" href="javascript:void(0)">
        <span>Categories</span>
        </a>
    </li>
    <li>
        <a onclick="validateLevelOfOwnership('MaintainDistributors.html');return false;" href="javascript:void(0)">
        <span>Distributors</span>
        </a>
    </li>
    <li>
        <a href="/recipeManager/distributorItem/listItems.action">
        <span>Distributor Items</span>
        </a>
    </li>
        <li>
        <a onclick="validateLevelOfOwnership('MaintainPreparationMethodsAndConversions.html');return false;" href="javascript:void(0)">
        <span>Prep Methods & Conversion Ratios</span>
        </a>
        </li>
    </ul>
</li>
4

2 回答 2

1

你可以试试下面的代码,

driver.findElement(By.linkText("Cookbooks")).click();

这可用于单击代表链接的文本..

于 2013-10-16T14:47:31.360 回答
0

您可以在此处使用“Actions”类来模拟用户手势。

下面是我编写的执行菜单点击的代码,我们需要先将鼠标悬停在根菜单上,然后点击子菜单。出于说明目的,我使用了以下站点: http: //www.milonic.com/menusample15.php

public static void main(String[] args) throws InterruptedException {

    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.milonic.com/menusample15.php");

    //element refering to 'Milonic' menu
    WebElement rootMenu = driver.findElement(By.linkText("Milonic"));

    Actions action = new Actions(driver);

    //move to 'Milonic' first
    action.moveToElement(rootMenu).perform();
    //wait for the sub-menu to come up
    Thread.sleep(1000);
    //inside sub-menu click on 'FAQ'
    action.moveToElement(driver.findElement(By.linkText("FAQ"))).click().perform();
}
于 2013-10-17T17:09:00.790 回答