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

Selenium:如何等待select中的选项被填充?,selenium,selenium-ide,Selenium,Selenium Ide,我第一次使用Selenium,我被这些选项弄得不知所措。我正在Firefox中使用IDE 当我的页面加载时,它随后通过JSONP请求获取值,并用该请求填充select中的选项 如何让Selenium在继续之前等待select中的某个选项出现?我认为您应该使用 waitForElementPresent命令。如果可能的话,让我看看您的selenium IDE代码。您可以使用WaitForSelectOption命令,其中您的值可以是直接标签 喜欢 label=1-储蓄账户 目标将具有我使用的带有c

我第一次使用Selenium,我被这些选项弄得不知所措。我正在Firefox中使用IDE

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


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

我认为您应该使用


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

您可以使用WaitForSelectOption命令,其中您的值可以是直接标签 喜欢 label=1-储蓄账户 目标将具有我使用的带有css目标的waitForElementPresent的对象id

示例:等待

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

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

命令:waitForElementPresent 目标:css=myselect选项[value=123] 值:将其留空
我用C编写了以下函数,在填充时返回select

您必须经过一段时间才能找到元素,并指定一段特定时间等待元素填充:

public static SelectElement FindSelectElementWhenPopulated(this IWebDriver driver, By by, int delayInSeconds)
    {
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(delayInSeconds));
        return wait.Until<SelectElement>(drv => 
        {
            SelectElement element = new SelectElement(drv.FindElement(by));
            if (element.Options.Count >= 2)
            {
                return element;
            }

            return null;
        }
        );
    }

在我的例子中,我验证select有两个以上的选项,您可以更改代码,使其验证符合您需要的数量。

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

Select select = new Select(driver.findElement(cssSelector("cssSelectorOfSelectBox")));    
waitUnitlSelectOptionsPopulated(select)
以下是WaitUnitLSelectOptions填充的定义

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>1,因为我们始终显示一个静态选项。

这对我来说很有用:

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

我发现了一些类似的东西:

wait.until(
    ExpectedConditions
        .presenceOfNestedElementsLocatedBy(By.id(<selectioNId>), By.tagName("option"))
)
使用Java8可等待性

请尝试下面的代码

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

当您只想等待在select元素中填充选项时,它非常有用。如何访问select类?它是从其他selenium Nuget软件包中获得的吗?这非常适合我的需要:我知道有一个select元素,但在检索元素之前,我需要了解该值是否存在于选项中,避免尝试查找不存在的put值。但是您不能将其与页面工厂一起使用……可以吗?
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