2016-01-26 135 views
0

我想从下拉一个值,然后单击使用硒的webdriver下一步按钮的下拉菜单和按钮。在Selenium IDE中它运行良好,但是当我尝试在Eclipse中使用相同的代码时,它不起作用。我尝试了不同的解决方案,但每次遇到Unable to locate element错误消息。下面是一个例子。硒的webdriver不能识别网页上

异常螺纹:"main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"name","selector":"formcode"}

解决方案我都试过,但从未工作:

1)

new Select(driver.findElement(By.name("formcode"))).selectByVisibleText("Weather Stopper(R) System Plus Ltd. Warranty"); 
      driver.findElement(By.name("vact")).click(); 

2)

WebDriverWait wait = new WebDriverWait(driver, 30); 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@name='formcode']"))); 

3)

new Select(driver.findElement(By.xpath("//select[@name='formcode']"))).selectByIndex(1); 
    driver.findElement(By.xpath("//td/input")).click(); 

的HTML代码是:

<tr> 
<TD><SELECT NAME="formcode" size="1"> 
<option value="">*** Select Warranty Type *** 
<OPTION VALUE="1PLUS48">Weather Stopper(R) System Plus Ltd. Warranty<OPTION VALUE="2GOLD52">Golden Pledge</SELECT></TD> 
<input type="hidden" name=low_slope_sqs value=""> 
<input type="hidden" name=addcode value=""> 
<input type="hidden" name=h_code_no value=""> 
<input type="hidden" name=steep_slope_sqs value=""> 
<input type="hidden" name="credit_account"><SPAN ID=messid></SPAN><table border=0 cellpadding=5 cellspacing=0 align=center> 
<TR> 
<TD> 
<input type=button name="vact" value="Next >>" onclick=submitit('Next1')></TD> 
<input type="hidden" name=entrydone value=""> 
</TR> 
</TABLE> 

enter image description here

+0

我没有在任何HTML下拉按钮,也没有看到。请发布您的完整html下拉和按钮。 – Guy

+0

嗨,我已经更新了html代码。你可以看看它吗?我从视图源中捕获了这些代码。 – user3259655

+0

'iframe'内的下拉菜单? – Guy

回答

0

要找到里面iframe元素,你需要切换到其第一

driver.switchTo.frame(frame ID); 
// or 
driver.switchTo.frame(driver.findElement(frameLocator)); 

并切换回

driver.switchTo().defaultContent(); 
// or 
driver.switchTo().window(driver.getWindowHandle()); 

作为一个方面说明,当你有很长的文字与像"Weather Stopper(R) System Plus Ltd. Warranty"独特的字符它更好地使用selectByValueselectByIndex。当文本选择select可能落在你错过,即使在一个空白。

+0

它像一个魅力工作。非常感谢你的帮助! – user3259655

+0

@ user3259655欢迎您:) – Guy