无法通过selenium webdriver点击下拉子菜单

时间:2014-10-08 08:50:54

标签: java javascript jquery selenium

我使用selenium webdriver自动化网站。我面临的问题是,当我点击菜单项时,子菜单打开,(它实际上是一个错位的下拉列表,ui问题),我可以找到我要点击的子菜单项的元素,但我不是能够点击,因为我得到一个例外:

"线程中的异常" main" org.openqa.selenium.ElementNotVisibleException:无法点击元素"

有什么方法可以点击这个子菜单项吗?

页面的HTML代码:

<td class="menulevel1norm" id="PanelTable" onmouseover="this.className='menulevel1hl';" onmouseout="this.className='menulevel1norm'" onclick="PopupWin('Left',divMenu20111219065812407304,this,'.menuitempopuprownormal','.menuitempopuprowhighlight','','.menuitempopupscroll');">
Text -  Inbound  
<div id="divMenu20111219065812407304" style="border-top-width: medium; border-right-width: medium; border-bottom-width: medium; border-left-width: medium; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; visibility: hidden; position: absolute;" name="actiondiv">
<div myonclick="window.parent.location.href='/smcfs/console/omreceipt.search';">
![enter image description here][4]Text - MENU_Receipt Console 

我的Webdriver for Javadriver:

 public static void main(String[] args) throws IOException, InterruptedException {
      Runtime rt = Runtime.getRuntime();
      rt.exec("taskkill /F /IM IEDriverServer.exe /T");
      WebDriver driver = UtilityMethods.driverinitialize();
      driver.get("xxxx");
      UtilityMethods.login(driver); 
      WebElement e1 = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.id("wicket-generated-id-4")));
        e1.click();
        driver.switchTo().defaultContent();
        driver.switchTo().frame("frame4");
        String eletext;
         List<WebElement> elements = driver.findElements(By.id("PanelTable"));
         for(WebElement ele : elements){
             eletext = ele.getText();
             System.out.println(eletext);
             if(eletext.equals(" Inbound ")){
                 ele.click();
                 break;
             }
         }

         Thread.sleep(2000);
         List<WebElement> elements2 = driver.findElements(By.tagName("div"));
         for(WebElement ele1 : elements2){
             eletext = ele1.getAttribute("myonclick");
             System.out.println(eletext);
             if(eletext == null){ 
                 continue; }
    else if(eletext.equals("window.parent.location.href='/smcfs/console/omreceipt.search';")){
                     ele1.click();
                    break;
             }
         }



}

1 个答案:

答案 0 :(得分:0)

首先,等待所有元素显示在页面上。然后,如果该元素正确可见并且不与另一个元素的其他部分重叠,则将执行单击。另一种方法是找到附近的适当元素,然后将指针移至某个偏移量,然后执行单击。