如何使用java(selenium web driver)从下拉列表中选择值

时间:2013-05-09 09:28:53

标签: java selenium selenium-webdriver automation webdriver

我需要从下拉列表中选择一个值可以帮助我。 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中获取值。

6 个答案:

答案 0 :(得分:4)

这应该适合你:

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

答案 1 :(得分: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");

而且,voilà!希望它有所帮助。

答案 2 :(得分: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) ;
    }
}

答案 3 :(得分:1)

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

希望它会对你有所帮助。

答案 4 :(得分: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");

        }
   }

答案 5 :(得分: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