1

我需要从下拉列表中选择一个值,有人可以帮助我。html部分如下

<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 中获取值。

4

6 回答 6

4

这应该适合你:

Select select = new Select(yourDropdownElement);
select.selectByVisibleText(text);
于 2013-05-09T09:34:12.203 回答
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的字符串和要查找的字符串即可。假设您需要获得Game,在您的场景中这很简单:

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

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

于 2013-05-09T12:35:48.693 回答
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) ;
    }
}
于 2013-05-09T09:35:06.660 回答
1
driver.findElement(By.id("element11_chzn")).click();
driver.findElement(By.id("element11_chzn_o_7")).click(); /*It will select 'Assessment' from drop down.*/

希望它会帮助你。

于 2013-07-04T07:38:51.347 回答
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");

        }
   }
于 2014-12-05T12:06:59.917 回答
0

用于打开浏览器、加载 URL 和从下拉列表中选择值的示例语句

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
于 2015-11-05T14:46:01.043 回答