2013-05-09 34 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 
相关问题