Selenium IEDriver发现了错误的元素

时间:2014-03-13 13:55:15

标签: java selenium webdriver

我的HTML

<ul class="level1 MenuItemClass static" tabindex="0" style="position: relative; width: auto; float: left;" role="menubar">
<li class="has-popup static" aria-haspopup="Menu1:submenu:17" role="menuitem" style="position: relative; float: left;">
    <a class="level1 MenuItemClass static" onclick="__doPostBack('mg200$Menu1','Menu1')" href="#" tabindex="-1">Menu1</a>
    <ul id="Menu1:submenu:17" class="level2 MenuItemClass dynamic" style="display: none; position: absolute; top: 100%; left: 0px;">
        <li class="dynamic" role="menuitem" style="position: relative;">
            <a class="level2 MenuItemClass selected dynamic" href="/page1.aspx" tabindex="-1">Submenu1</a>
        </li>
    </ul>
</li>
<li class="has-popup static" aria-haspopup="Menu1:submenu:22" role="menuitem" style="position: relative; float: left;">
    <a class="level1 MenuItemClass static" onclick="__doPostBack('mg200$Menu1','Menu2')" href="#" tabindex="-1">menu2</a>
    <ul id="Menu1:submenu:22" class="level2 MenuItemClass dynamic" style="display: none; position: absolute; top: 100%; left: 0px;">
        <li class="dynamic" role="menuitem" style="position: relative;">
            <a class="level2 MenuItemClass dynamic" href="/page2.aspx" tabindex="-1">Submenu2</a>
        </li>
    </ul>
</li>
</ul>

我的代码:

...
            moveToElement(By.xpath("//a[contains(text(), 'Menu1')]"));
            moveToElement(By.xpath("//a[contains(text(), 'Submenu1')]"));
            clickWhenTheElementIsClickable(By.xpath("//a[contains(text(), 'Submenu1')]"), 10);
...
        public void moveToElement(By locator) {
            Actions actions = new Actions(driver);
            actions.moveToElement(driver.findElement(locator)).perform();           
        }   

        protected void clickWhenTheElementIsClickable(By locator, long timeout) {
            WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver,timeout)
            .ignoring(StaleElementReferenceException.class);
            WebElement element = wait.until(
                    ExpectedConditions.elementToBeClickable(locator));
            element.click();
        }       
...

行:

moveToElement(By.xpath("//a[contains(text(), 'Submenu1')]"));

的工作原理为:

moveToElement(By.xpath("//a[contains(text(), 'menu2')]"));

所以我期待点击“Submenu1”。但光标移动到“Menu2”。 使用IEDriver 2.38.0 Firefox运行正常。 有任何想法吗? 尝试清理项目。没有帮助。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 在“子菜单1”中出现了一个奇怪的区域冲突。和&#39;子菜单2&#39;元素。 所以,我编辑了我的moveToElement()方法:

public void moveToElement(By locator) {
    Actions actions = new Actions(driver);
    actions.moveToElement(driver.findElement(locator), 1, 1).perform();         
}   

现在它在Firefox和IE中运行良好。 希望,这将有助于smb。