2016-01-26 62 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