如何使用java(硒网络驱动程序)从下拉列表中选择值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16458538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:00:55  来源:igfitidea点击:

How to select value from dropdown using java (selenium web driver)

javaseleniumselenium-webdriverautomationwebdriver

提问by vibha

I need to select a value from dropdown can some one help me on that. Html part is below

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

I have to make it dynamic so I need to get the value from xls.

我必须让它动态,所以我需要从 xls 获取值。

回答by Danstahr

This should work for you :

这应该适合你:

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

回答by aran

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) ;
    }
}

回答by aimbire

Since you are not using actually a element, i'd use the following code that should suit perfectly in that scenario. That should click in case a element finds the correct text inside the <li>element.

由于您实际上并没有使用元素,因此我将使用以下应该完全适合该场景的代码。如果元素在元素内找到正确的文本,则应该单击<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;
        }
    }
}

To use this method, all you need to do send the proper WebElementand the String you're looking to find. Say you need to get Game, in your scenario that's easy as:

要使用此方法,您只需发送正确WebElement的字符串和您要查找的字符串。假设您需要获取Game,在您的场景中很简单:

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

And, voilà! Hope it helps.

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

回答by Soumya Vaishnavi

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

Hope it will help you.

希望它会帮助你。

回答by Helping Hands

Sample program to get value from drop down :

从下拉列表中获取值的示例程序:

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");

        }
   }

回答by Krushna Chulet

Sample statements to open browser, load URL and select value from dropdown

打开浏览器、加载 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