3

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

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 准备好进行通信的任何方法吗?

4

3 回答 3

3

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

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

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

于 2015-04-16T11:50:26.530 回答
1

如果您不想使用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)

于 2013-01-22T13:40:59.433 回答
0

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

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

于 2016-08-31T11:56:45.397 回答