如何等待直到使用Selenium Webdriver加载下拉列表? [英] How to wait until a dropdown is loaded using selenium webdriver?

查看:192
本文介绍了如何等待直到使用Selenium Webdriver加载下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有2个下拉菜单.

i have 2 dropdown menus on a page.

第一个下拉列表:

First dropdown:

<select name="ddlColor_N" onchange="javascript:setTimeout('__doPostBack(\'ddlColor_N\',\'\')', 0)" id="ddlColor_N" class="Searchddl" style="text-decoration: none; width: 152px; height: 22px;">
        <option selected="selected" value="">Select</option>
        <option value="91_CC_A">Red</option>
        <option value="248_PS_A">Green</option>
        <option value="27_CG_A">Yellow</option>
    </select>

第二个下拉列表

Second dropdown

<select name="ddlFruit_N" id="ddlFruit_N" class="Searchddl" style="text-decoration: none; width: 152px; height: 22px;">
    <option value="">Select</option>
    <option value="447">Grapes</option>
<option value="448">Mango</option>
<option value="449">Apple</option>
</select>

当我在第一个下拉列表中单击红色时,然后在第二个下拉列表中,苹果将在几秒钟后自动加载.

When i click on Red in first dropdown, then in second dropdown Apple will be automatically loaded after couple of seconds.

这是我的问题,我该如何等待第二个下拉列表完全加载.

Here comes my problem, that how can i wait for the second dropdown to load fully.

Am使用C# 谢谢

推荐答案

请尝试以下代码.我们将检查要加载的选项.

Please try below code. We will check for options to load.

public static SelectElement FindSelectElementWhenPopulated(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;
        }
        );
    }

我这样称呼它:

Myclass.FindSelectElementWhenPopulated(driver, By.CssSelector("#ddlFruit_N"), 20);

这篇关于如何等待直到使用Selenium Webdriver加载下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆