无法将鼠标悬停在菜单上以使其在Selenium中崩溃

时间:2018-09-27 12:46:56

标签: selenium selenium-webdriver webdriver

有一个菜单按钮,其ID为“ usermenucollapsed”。我想将鼠标悬停在该菜单上,以便出现菜单并提供注销选项。我已经标识了带有“ id”的菜单,并在其上使用了moveToElement()来将菜单悬停在菜单上,但菜单没有运气。我已检查菜单按钮不在框架中,以确保不需要切换到框架。

isDisplayed()返回false,而isEnabled返回true。

<div class="mobile-bar">
        <ul id="toolbar" class="toolbar">
            <li id="quickcreatetop" class="create dropdown nav navbar-nav quickcreatetop">
                <a class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                    CREATE
                </a>
                <ul class="dropdown-menu" role="menu">
                    <li>
                        <a href="index.php?module=Accounts&action=EditView&return_module=Accounts&return_action=DetailView">Create Account</a>
                    </li>
                    <li>
                        <a href="index.php?module=Contacts&action=EditView&return_module=Contacts&return_action=DetailView">Create Contact</a>
                    </li>
                    <li>
                        <a href="index.php?module=Opportunities&action=EditView&return_module=Opportunities&return_action=DetailView">Create Opportunity</a>
                    </li>
                    <li>
                        <a href="index.php?module=Leads&action=EditView&return_module=Leads&return_action=DetailView">Create Lead</a>
                    </li>
                    <li>
                        <a href="index.php?module=Documents&action=EditView&return_module=Documents&return_action=DetailView">Create Document</a>
                    </li>
                    <li>
                        <a href="index.php?module=Calls&action=EditView&return_module=Calls&return_action=DetailView">Log Call</a>
                    </li>
                    <li class="last">
                        <a href="index.php?module=Tasks&action=EditView&return_module=Tasks&return_action=DetailView">Create Task</a>
                    </li>
                </ul>
            </li>
            <li id="" class="dropdown nav navbar-nav navbar-search">
                <button id="searchbutton" class="dropdown-toggle btn btn-default searchbutton" data-toggle="dropdown" aria-expanded="true">
                    <!--<span class="glyphicon glyphicon-search"> </span>-->Search
                </button>
                <div class="dropdown-menu" role="menu" aria-labelledby="searchbutton">
                    <form id="searchformdropdown" class="searchformdropdown" name='UnifiedSearch' action='index.php'
                          onsubmit='return SUGAR.unifiedSearchAdvanced.checkUsaAdvanced()'>
                        <input type="hidden" class="form-control" name="action" value="UnifiedSearch">
                        <input type="hidden" class="form-control" name="module" value="Home">
                        <input type="hidden" class="form-control" name="search_form" value="false">
                        <input type="hidden" class="form-control" name="advanced" value="false">
                        <div class="input-group">
                            <input type="text" class="form-control query_string" name="query_string" id="query_string"
                                   placeholder="Search..." value=""/>
                        <span class="input-group-btn">
                            <button type="submit" class="btn btn-default"><!--<span class="glyphicon glyphicon-search"
                                                                                aria-hidden="true"></span>-->Search</button>
                        </span>
                        </div>
                    </form>
                </div>
            </li>
            <li id="desktop_notifications" class="dropdown nav navbar-nav desktop_notifications">
                <button class="alertsButton btn dropdown-toggle" data-toggle="dropdown"
                        aria-expanded="false">
                    <span class="alert_count hidden">0</span>
                </button>
                <div id="alerts" class="dropdown-menu" role="menu">This information is not available</div>
            </li>
            <li>
                <form id="searchform" class="navbar-form searchform" name='UnifiedSearch' action='index.php'
                      onsubmit='return SUGAR.unifiedSearchAdvanced.checkUsaAdvanced()'>
                    <input type="hidden" class="form-control" name="action" value="UnifiedSearch">
                    <input type="hidden" class="form-control" name="module" value="Home">
                    <input type="hidden" class="form-control" name="search_form" value="false">
                    <input type="hidden" class="form-control" name="advanced" value="false">
                    <div class="input-group">
                        <input type="text" class="form-control query_string" name="query_string" id="query_string"
                               placeholder="Search..." value=""/>
                <span class="input-group-btn">
                    <button type="submit" class="btn btn-default"><!--<span class="glyphicon glyphicon-search"
                                                                        aria-hidden="true"></span>-->Search</button>
                </span>
                    </div>
                </form>
            </li>
            <li id="globalLinks" class="dropdown nav navbar-nav globalLinks-mobile">

必需的按钮是这个。

<button id="usermenucollapsed" class="dropdown-toggle btn btn-default usermenucollapsed" data-toggle="dropdown"
                        aria-expanded="true">
                </button>

                    

WebElement menu = driver.findElement(By.id("usermenucollapsed"));
Actions builder = new Actions(driver);
builder.moveToElement(menu).build().perform();

1 个答案:

答案 0 :(得分:0)

这不完全是您要的内容,但是根据我的经验,在这种情况下,最简单的方法是仅使用javascript单击注销按钮,而不必首先将鼠标悬停在菜单上。您可以执行以下操作:

WebElement element = driver.findElement(By.id("logout button selector here"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);