【问题标题】:How to select value from dropdown using java (selenium web driver)如何使用 java(selenium web 驱动程序)从下拉列表中选择值
【发布时间】:2013-05-03 17:42:32
【问题描述】:

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

【问题讨论】:

标签: java selenium selenium-webdriver automation webdriver


【解决方案1】:

这应该适合你:

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

【讨论】:

  • 这行不通,因为它不是
  • 我收到了这个错误 org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: Element cannot be scrolled into view:[object HTMLInputElement]
【解决方案2】:

由于您实际上并未使用元素,因此我将使用以下代码,该代码应该非常适合该场景。如果元素在 &lt;li&gt; 元素中找到正确的文本,则应该单击它。

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

还有,!希望对您有所帮助。

【讨论】:

    【解决方案3】:
    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】:
      driver.findElement(By.id("element11_chzn")).click();
      driver.findElement(By.id("element11_chzn_o_7")).click(); /*It will select 'Assessment' from drop down.*/
      

      希望对你有帮助。

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

        • 请注意,我们希望得到解决问题中具体问题的答案。如果您有一个非常相关的替代方案,您可以考虑添加它 - 但请务必首先解决问题并准确解释原因。
        【解决方案6】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-18
          • 1970-01-01
          • 1970-01-01
          • 2016-02-04
          • 2014-06-06
          • 1970-01-01
          相关资源
          最近更新 更多