2014-03-13 58 views
0

我的HTML:硒IEDriver發現錯誤的元素

<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工作正常。 任何想法? 試圖清理項目。沒有幫助。

+0

當你在測試中到達那裏,你的所有菜單是否打開(顯示它們的子菜單)? – Louis

+0

Yes, after「moveToElement(By.xpath(」// a [contains(text(),'Menu1')]「));」 Submenu1顯示一段時間, 然後光標變爲「Menu2」 –

回答

0

我找到了解決方案。 'Submenu1'和'Submenu2'元素有一個奇怪的區域衝突。 所以,我編輯moveToElement()方法:

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

現在它工作正常在Firefox和IE。 希望,這將有助於smb。