2013-05-09 88 views
1

我需要從下拉列表中選擇一個值,有人可以幫我解決這個問題。 HTML部分低於如何使用java(硒web驅動程序)從下拉列表中選擇值

<div id="element11_chzn" class="chzn-container chzn-container-single" style="width: 320px;"> 
<a class="chzn-single" href="javascript:void(0)" tabindex="0"> 
<div class="chzn-drop" style="left: -9000px; width: 318px; top: 29px;"> 
<div class="chzn-search">`enter code here` 
<ul class="chzn-results"> 
<li id="element11_chzn_o_1" class="active-result" style="">ActiveLearn Course</li> 
<li id="element11_chzn_o_2" class="active-result" style="">ActiveLearn Player</li> 
<li id="element11_chzn_o_3" class="active-result result-selected" style="">ActiveLearn Skin</li> 
<li id="element11_chzn_o_4" class="active-result" style="">ActiveLearn Template</li> 
<li id="element11_chzn_o_5" class="active-result" style="">Activity</li> 
<li id="element11_chzn_o_6" class="active-result" style="">Animation</li> 
<li id="element11_chzn_o_7" class="active-result" style="">Assessment</li> 
<li id="element11_chzn_o_8" class="active-result" style="">Bookmarks</li> 
<li id="element11_chzn_o_9" class="active-result" style="">Character</li> 
<li id="element11_chzn_o_10" class="active-result" style="">Click to prompt</li> 
<li id="element11_chzn_o_11" class="active-result" style="">Click to prompt override</li> 
<li id="element11_chzn_o_12" class="active-result" style="">EBook</li> 
<li id="element11_chzn_o_13" class="active-result" style="">Exercise</li> 
<li id="element11_chzn_o_14" class="active-result" style="">Game</li> 
<li id="element11_chzn_o_15" class="active-result" style="">Glossary</li> 
<li id="element11_chzn_o_16" class="active-result" style="">Glossary Term</li> 
<li id="element11_chzn_o_17" class="active-result" style="">Glossary Term</li> 
<li id="element11_chzn_o_18" class="active-result" style="">Imported file</li> 
<li id="element11_chzn_o_19" class="active-result" style="">Interactive activity</li> 
<li id="element11_chzn_o_20" class="active-result" style="">Interactive page</li> 

</ul> 
</div> 

我必須讓它動態的,所以我需要從XLS值。

+0

的可能重複http://stackoverflow.com/打開瀏覽器,負載路徑,選擇價值問題/ 11343017/how-to-click-an-option-element-with-webdriver – luksch 2013-05-09 09:35:15

+0

這會幫助你http://santoshsarmajv.blogspot.in/2013/04/Select.html – Santoshsarma 2013-05-09 10:17:43

+0

你們有沒有讀過代碼? – aimbire 2013-05-09 12:22:06

回答

4

這應該爲你工作:

Select select = new Select(yourDropdownElement); 
select.selectByVisibleText(text); 
+2

這是行不通的,因爲它不是一個 aimbire 2013-05-09 12:21:08

+0

我得到這個錯誤org.openqa.selenium.interactions.MoveTargetOutOfBoundsException:元素無法滾動到視圖中:[object HTMLInputElement] – vibha 2013-05-09 12:29:07

1
var ul = document.getElementById("chzn-results"); 
var liArray = ul.getElementsByTagName("li"); 

for (var i = 0; i < liArray.length; i++) { 
    { 
     //where liArray[i] being the LI element on the position (i) ; 
    } 
} 
4

既然你不使用實際上是一個元素,我會使用下面的代碼應該在那種情況下完全滿足。如果元素在<li>元素內找到正確的文本,應該點擊。

public void selectValueFromUnorderedList(WebElement unorderedList, final String value) { 
    List<WebElement> options = unorderedList.findElements(By.tagName("li")); 

    for (WebElement option : options) { 
     if (value.equals(option.getText())) { 
      option.click(); 
      break; 
     } 
    } 
} 

要使用這個方法,所有你需要做派你希望找到合適的WebElement和字符串。假設你需要得到遊戲,在您的情況,很容易爲:

WebElement ul = driver.findElement(By.className("chzn-results")); 
selectValueFromUnorderedList(ul, "Game"); 

而且,!希望能幫助到你。

1
driver.findElement(By.id("element11_chzn")).click(); 
driver.findElement(By.id("element11_chzn_o_7")).click(); /*It will select 'Assessment' from drop down.*/ 

希望它能幫助你。

0

示例程序,從下拉菜單中獲取價值:

public class demo { 


     public static void main(String[] args) throws IOException, InterruptedException { 


     FirefoxDriver driver = new FirefoxDriver(); 

     //OPEN SPECIFIC URL IN BROWSER 
     driver.get("http://www.toolsqa.com/automation-practice-form/"); 


     //SELECT SPECIFIC VALUE FROM DROPDOWN 
     Select sel = new Select(driver.findElement(By.id("continents"))); 
     sel.selectByVisibleText("Australia"); 

     } 
    } 
+0

請請注意,我們希望解決問題中解決*特定問題的答案。如果你有一個非常相關的選擇,你*可以*考慮添加 - 但請務必解決這個問題並首先解釋爲什麼。 – 2014-12-10 16:52:53

0

樣品語句從下拉

static WebDriver driver; 
System.setProperty("webdriver.ie.driver","C:\\(Path)\\IEDriverServer.exe"); 
driver = new InternetExplorerDriver(); 
driver.manage().window().maximize(); 

driver.get("EnterURLHere");   
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 

Select value1 = new Select(driver.findElement(By.id("element11_chzn")));  
value1.selectByVisibleText("Character"); //Select Character from dropdown list 
相關問題