2015-11-23 29 views
0

This select fields has same name如何選擇這硒具有相同名稱不同的下拉列表中的webdriver

錯誤顯示爲

org.openqa.selenium.remote.RemoteWebElement cannot be cast to org.openqa.selenium.support.ui.Select 

這裏是我的代碼:

List<WebElement> ref = driver.findElements(By.name("customerBean.relationCd")); 
System.out.println("reference dropdowns " + ref.size()); 
((Select) ref.get(0)).selectByIndex(18); 
((Select) ref.get(1)).selectByIndex(18); 
+0

你可以發佈一些你正在使用的網頁的HTML嗎?這兩者之間可能有區別,可以幫助我們找到一個比名字更好的獨特選擇器。 –

回答

1

與您的代碼的問題是,你具有輸入到Select對象的WebElement對象。

((Select) ref.get(0)).selectByIndex(18); 

這不是如何做到的。 Select對象應該與webelement中定義的參數分開使用,而不是通過類型轉換。

List<WebElement> ref = driver.findElements(By.name("customerBean.relationCd")); 
System.out.println("reference dropdowns " + ref.size()); 
Select s; 

s = new Select(ref.get(0)); 
s.selectByIndex(18); 

s = new Select(ref.get(1)); 
s.selectByIndex(18); 

希望能幫助你,讓我知道你是否有更多的疑問。

+0

感謝manu,我試過了它的代碼,它的工作。 –

相關問題