我有一个下拉列表,如下图所示。单击“更多...”选项将加载下十个数据。下次点击更多时,将加载另外10个数据。类似地,加载整个列表。加载所有内容后如何访问整个列表。

javascript - 如何从动态加载的选择框中获取数据-LMLPHP

  Select select=new Select(element);
        List<WebElement> options= select.getOptions();
int j=0;
    for(WebElement ele:options)
                {
                    str=ele.getText();
                    if(str.contains("More"))
                    {
                     //this is to click the "More.." using selenium
                    Actions action = new Actions(BaseClass.driver);
                    action.moveToElement(Filter.FilterApplied(),200, 0).click().build().perform();
                    Filter.FilterApplied().sendKeys(str);
                    action.moveToElement(Filter.FilterApplied()).click().build().perform();

                    //to assign newly loaded list to a new array
                   Select  se=new Select(Filter.FilterApplied());
                    options2=se.getOptions();
                    System.out.println("size of new list"+options2.size());

                    }
                    }


使用此代码,我可以单击并在页面中加载一次列表。.但是无法将新加载的列表复制到另一个对象。 options2.size()给出的旧arraylist大小与旧数组列表再次加载到options2中的大小相同。为什么呢?新的arraylist应该加载正确吗?
我用硒尝试过的。任何方法都可以..也许JQuery有一个更有效的方法?请告诉我。我只需要访问列表,该列表最初并未完全加载到页面中,而是在单击更多按钮后加载。

HTML:

<select id="filterSel" name="filterSel" class="fixed-size" onchange="fnLoadAccountslegEnt(this.value); " onclick="closeDropDown();" size="1" style="top: 6.5px; margin: 0px; width: 58%; height: 30px;">
<option value=""></option>
<option value="a">Account:00</option>
<option value="a0">Account:0</option>
<option value="More" style="color: blue;">More...</option>
</select>


最初将在此处。在单击“更多”时,正在加载其他选项,例如这样。它的标题比其他选项多:

<select id="filterSel" name="filterSel" class="fixed-size" onchange="fnLoadAccountslegEnt(this.value); " onclick="closeDropDown();" size="1" style="top: 6.5px; margin: 0px; width: 58%; height: 30px;">
<option value=""></option>
<option value="a">Account:00</option>
<option value="a0">Account:0</option>
<option value="a1" title="Account: 1">Account: 1</option>
<option value="a2" title="Account: 10 ">Account: 10 </option>
<option value="More" style="color: blue;">More...</option>
</select>

最佳答案

您可以使用以下方法单击dropDown中出现的所有More选项:

public static void clickMoreDropDownElement(WebDriver driver){

    Select select=new Select(driver.findElement(By.id("filterSel")));

    //while(select.getOptions().contains("More")){

        List<WebElement> allElements = select.getOptions();

        Iterator<WebElement> itr = allElements.iterator();

        while(itr.hasNext()){

            WebElement currentElement = itr.next();


            if(currentElement.getText().equals("More...")){

                currentElement.click();

                System.out.println("Clicked More..");

                clickMoreDropDownElement(driver);
            }
        }

}


完成此操作后,您可以按以下方式存储所有元素:

List<WebElement> options= select.getOptions();

09-10 03:12
查看更多