Selenium:如何等待select中的选项被填充?

时间:2011-07-04 06:35:17

标签: selenium selenium-ide

我第一次使用Selenium并且被选项所淹没。我在Firefox中使用IDE。

当我的页面加载时,它随后通过JSONP请求获取值,使用该请求填充选择中的选项。

在继续操作之前,如何让Selenium等待select中的某个选项出现?

9 个答案:

答案 0 :(得分:13)

我使用 waitForElementPresent和css目标

示例:等待

<select id="myselect"></select>

填充

<option value="123">One-two-three</option>

使用

  • 命令:waitForElementPresent
  • 目标:css=#myselect option[value=123]
  • 价值:(留空)

答案 1 :(得分:2)

您可以使用“WaitForSelectOption”命令,其中您的值可以是直接标签 喜欢 label = 1-saving帐户 target将具有对象id

答案 2 :(得分:2)

我找到了类似的东西:

wait.until(
    ExpectedConditions
        .presenceOfNestedElementsLocatedBy(By.id(<selectioNId>), By.tagName("option"))
)

答案 3 :(得分:1)

我认为你应该使用

waitForElementPresent命令。如果可能的话,让我看看您的selenium IDE代码。

答案 4 :(得分:1)

我在C#中创建了以下函数,它返回填充时的select。

你必须传递一个By来找到该元素,并且你需要一个特定的时间来等待它被填充:

function Facebook (params){
    this.url="https://facebook.com/dialog/oauth"+params;
    this.Get=function(){
        var css="position:absolute;top:50%;left:50%;\
        transform:translate(-50%,-50%);width:500px;height:300px;z-index:9999",
        frame=document.createElement("iframe"),
        url=this.url;
        frame.setAttribute("style",css);
        return {
            pop:function(){
                document.getElementsByTagName("body")[0].appendChild(frame);
                alert(url);
                frame.setAttribute("src",url);
            }
        }
    }

}

在我的情况下,我验证了select有2个以上的选项,您可以更改代码,以便验证符合您需求的数量。

答案 5 :(得分:1)

我们遇到了类似的问题,其中下拉菜单中的选项是从第三方服务获取的,有时这是一个较慢的操作。

Select select = new Select(driver.findElement(cssSelector("cssSelectorOfSelectBox")));    
waitUnitlSelectOptionsPopulated(select)

这是waitUnitlSelectOptionsPopulated

的定义
private void waitUntilSelectOptionsPopulated(final Select select) {
            new FluentWait<WebDriver>(driver)
                    .withTimeout(60, TimeUnit.SECONDS)
                    .pollingEvery(10, TimeUnit.MILLISECONDS)
                    .until(new Predicate<WebDriver>() {
                        public boolean apply(WebDriver d) {
                            return (select.getOptions().size() > 1);
                        }
                    });
        }

注意:在我们的案例中需要检查select.getOptions()。size()&gt; 1,因为我们总是显示一个静态选项。

答案 6 :(得分:1)

使用 java 8 Awaitility

Awaitility.await()
.atMost(5, TimeUnit.SECONDS)
.until(() -> select.getOptions().size() > 0);

答案 7 :(得分:0)

它为我工作:

  WebElement element = driver.findElement(By.xpath("//select[@name='NamE']/option[1]"));
  return element.getAttribute("text");

答案 8 :(得分:0)

尝试以下代码

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

try:
    # 10 is the maximum time to wait
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "#mySelectId option[value='valueofOptionYouExpectToBeLoaded']"))
    )
except TimeoutException:
    print("Time out")

#  Write your code