【问题标题】:Selenium, wait for element, page object model, c# bindingsSelenium,等待元素,页面对象模型,c#绑定
【发布时间】:2013-01-05 13:20:07
【问题描述】:

我们如何等待 IWebElement 被(重新)附加到 DOM?我的场景是这样的,我从 dropdown1 中选择一个值,然后单击数据绑定发生在 dropdown2 上。因此,当我的测试类似于从 Dd1 中选择“foo”,然后从 Dd2 中选择“bar”时 -> 我会得到一个异常,因为 Dd2 尚未呈现,所以存在竞争条件。 现在,我知道,我们有 WebDriverWait 类,我们可以像这样使用 Until 方法:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
wait.Until(By.Id("foo"));

但我真的不想将定位符字符串(“foo”)带入我的测试逻辑,因为它似乎违背了使用页面对象模型的意义。使用 Page 对象模型时,我已经有了 IWebElement 实例

[FindsBy(How = How.Id, Using = "ctl00_MainContentPlaceHolder_actionButtonBarControl_btnSave")]
    public IWebElement BtnSave { get; set; }

那么,您知道隐式等待 IWebElement 准备好进行通信的任何方法吗?

【问题讨论】:

  • 所以目前我使用它如下,但仍在寻找好的解决方案
  • internal static void WaitForElementToBeReadyAndSelectByText(IWebElement webElement, string s) { for (var i = 0; i
  • 您找到了一个好的解决方案吗?我正在为同样的事情苦苦挣扎。

标签: c# selenium webdriver automated-tests selenium-webdriver


【解决方案1】:

您好,我知道它有点晚了,但我遇到了同样的问题,我通过执行以下操作(使用您的代码)解决了这个问题:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
wait.Until(By.Id(_pageModel.BtnSave.GetAttribute("id"));

然后它只返回 ID 属性的值,并阻止您使用元素查找来污染您的测试代码。希望能帮助到你。

【讨论】:

    【解决方案2】:

    如果您不想使用locator values,则意味着您可以使用隐式等待而不是使用显式等待。隐式等待也使driver 实例等待给定的时间段。

    Actual difference to Explicit Wait is that it will tell Web driver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.The default setting is 0.

    代码:

      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    

    要记住的一点是,一旦设置了隐式等待,它将在 WebDriver 对象实例的整个生命周期内保持不变。

    欲了解更多信息,请使用此链接http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html#implicitlyWait(long,java.util.concurrent.TimeUnit)

    【讨论】:

      【解决方案3】:

      我们所做的是传入一个原始的选择器文本,例如 seleniumRC css=a, xpath=b。

      然后我们有一个findElement 方法来解析请求,获取适当的By 并搜索元素

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-12
        • 1970-01-01
        • 1970-01-01
        • 2018-06-14
        相关资源
        最近更新 更多