Selenium有时成功点击子菜单项

时间:2014-12-01 00:21:48

标签: selenium selenium-webdriver

我正在使用Selenium的其中一个场景,它有时会起作用。

我关注的流程非常简单,已经多次询问和回答。

将鼠标悬停在主菜单项上 移动并单击子菜单项。

以下是代码:

关于我每次如何做这项工作的任何建议都将非常感激。

var actions = new Actions(SeleniumTestDriver.WebDriver);
// Move to the Main Menu Element and hover  
actions.MoveToElement(SeleniumTestDriver.WebDriver.FindElement(By.XPath(@"//*[@id='main_menu']/ul/li[3]/a"))).Perform();

Thread.Sleep(1000); 

var wait = new WebDriverWait(SeleniumTestDriver.WebDriver, TimeSpan.FromSeconds(5));
var subMenuLink = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(@"//*[@id='main_menu']/ul/li[3]/ul/li[4]/a")));

Thread.Sleep(250);
actions.MoveToElement(subMenuLink).Click().Perform();

版本详细信息: Firefox v33.1 Selenium.WebDriver 2.44.0 Selenium.Support 2.44.0 语言C#

按操作编辑

我还应该补充说,当它失败时,菜单会闪烁,好像悬停已被中断。打开和关闭太快,无法单击子菜单项。

该菜单的Html是:

<nav id="main_menu" class="ddsmoothmenu">
    <ul class="primary_menu">
        <li><a href="/webinar/calendar">calendar</a></li>
        <li class="parent" style="z-index: 100;">...</li>
        <li class="parent" style="z-index: 99;">
            <a href="javascript:void(0)" class="">Upcoming Webinars<i></i></a>
            <ul style="top: 95px; visibility: visible; left: 0px; width: 195px; display: none;">
                <li role="presentation">
                    <a role="menuitem" tabindex="-1" href="/Webinar/Details/4567">Best-Ever Compliance Checklists For...</a>
                </li>
                <li role="presentation">
                    <a role="menuitem" tabindex="-1" href="/Webinar/Details/4572">Build a No-Excuses Sales Environmen...</a>
                </li>
                <li role="presentation">
                    <a role="menuitem" tabindex="-1" href="/Webinar/Details/4560">Handling Power of Attorney Document...</a>
                </li>
                <li role="presentation">
                    <a role="menuitem" tabindex="-1" href="/Webinar/Details/4566">Flood Insurance</a>
                </li>
                <li role="presentation">
                    <a role="menuitem" tabindex="-1" href="/Webinar/Details/4562">Opening Accounts for Nonresident Al...</a>
                </li>
                <li role="presentation">
                    <a role="menuitem" tabindex="-1" href="/Webinar/Details/4561">New Share Member Account Interview ...</a>
                </li>
                <li role="presentation">
                    <a role="menuitem" tabindex="-1" href="/Webinar/allActive/?eventsToShow=upcoming">
                        <font color="green">View <b>All</b> Upcoming Events</font>
                    </a>
                </li>
            </ul>
        </li>
        <li class="parent" style="z-index: 98;">...</li>
        <li style="z-index: 97;">
            <a href="javascript:void(0)" class="">About Us<i></i></a>
            <ul style="display: none; top: 95px; visibility: visible;">
                <li role="presentation"><a role="menuitem" tabindex="-1" href="/Home/WhatIsAWebinar">What Is A Webinar?</a></li>
                <li role="presentation"><a role="menuitem" tabindex="-1" href="/Home/CommonQuestions">Commonly Asked Questions</a></li>
                <li role="presentation"><a role="menuitem" tabindex="-1" href="/Home/DetailedConnectionInstructions">Connecting to Your Webinar</a></li>
                <li role="presentation"><a role="menuitem" tabindex="-1" href="/Home/contactus">Contact Us</a></li>
            </ul>
        </li>       
    </ul>
</nav>

由于

2 个答案:

答案 0 :(得分:1)

我修改了现有代码的xpath。请检查一下是否适合您:

var actions = new Actions(SeleniumTestDriver.WebDriver);
// Move to the Main Menu Element and hover  
actions.MoveToElement(SeleniumTestDriver.WebDriver.FindElement(By.XPath("//li[@class='parent']/a[.='Upcoming Webinars']"))).Build().Perform();

var wait = new WebDriverWait(SeleniumTestDriver.WebDriver, TimeSpan.FromSeconds(10));
var subMenuLink = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//li[@role='presentation']/a[contains(text(),'Flood Insurance')]")));

actions.MoveToElement(subMenuLink).Click().Perform();

答案 1 :(得分:0)

请使用以下代码进行试用:

WebElement web = dri.findElement(By
        .xpath("//a[contains(.,'Upcoming Webinars')]"));
Actions objA = new Actions(dri);
objA.moveToElement(web).click().build().perform();
objA.moveToElement(
    (new WebDriverWait(dri, 3)).until(ExpectedConditions.elementToBeClickable(By
        .xpath("//a[contains(text(),'Safe Deposit Boxes')]"))))
    .click().build().perform();

您可以使用您自己选择的元素替换Safe Deposit Boxes

<强> P.S。对不起,但是这段代码是用java编写的,请自助了解C#中的任何等价物。