通过selenium网络驱动程序点击下拉子菜单时出现问题

trouble clicking on dropdown sub-menu through selenium webdriver

本文关键字:菜单 问题 网络 selenium 驱动程序 通过      更新时间:2023-09-26

我正在使用selenium网络驱动程序自动化一个网站。我面临的问题是,当我点击一个菜单项时,子菜单会打开(这实际上是一个错位的下拉菜单,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 

我的Web驱动程序Java代码:

 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;
             }
         }

}

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