【发布时间】:2014-10-08 08:50:54
【问题描述】:
我正在使用 selenium webdriver 自动化网站。我面临的问题是,当我点击一个菜单项时,子菜单打开,(它实际上是一个放错位置的下拉菜单,ui 问题),我可以找到我想要点击的子菜单项的元素,但我不是当我遇到异常时可以点击:
“线程“主”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 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;
}
}
}
【问题讨论】:
-
您能说明一下您想点击哪些元素吗?根据您上面的代码,我猜是
ele1,但一些指导可能会帮助我们帮助您。谢谢。 -
嗨,马克,我很想点击带有文本“MENU_Receipt Console”的子菜单,是的,我已经搜索了它的属性 myonclick,我正在通过 WebElement 变量 ele1 点击它。
-
所以你点击ID为
PanelTable的元素,)页面上不止一个吗?我猜这是由于您的 for 循环,但认为最好检查),这揭示了 2 个div元素,您想单击第二个元素,但您得到的异常是它不可见?再次抱歉试图完全理解问题。 -
是的,有多个元素具有相同的 id 和标签,所以我用它们的独特属性来识别它们,找到了元素,但它没有点击它,并给出了异常:(
-
它可以找到正确的元素但无法点击它,这似乎很奇怪。你能展示一下它的屏幕截图吗?
标签: java javascript jquery selenium