【问题标题】:How to not wait for a page load after SelectElement.SelectByValue()如何在 SelectElement.SelectByValue() 之后不等待页面加载
【发布时间】:2012-11-29 14:44:29
【问题描述】:

给定一个带有<select> 元素和一些<option>s 的页面,当点击该页面时会导致浏览器打开一个新的URL,例如(JavaScript 代码省略):

<select>
  <option value="here">go here</option>
  <option value="there">go there</option>
</select>

当我使用 WebDriver 自动执行此行为时,我会调用

hereOrThereSelectElement.SelectByValue("here");

然后等待新页面加载完毕。

在我的例子中,新位置的页面加载需要超过 60 秒(故意),所以我不得不增加 WebDriver 实例的命令超时。不幸的是,此超时将影响使用同一 WebDriver 实例创建的所有其他测试。

我还想执行显式等待新页面加载(以将其标记为在测试场景中长时间运行),但在这种情况下,将在 SelectByValue 命令成功后调用下一个命令 - 所以不再需要等待。

我查看了源代码,发现SelectByValue 命令使用了Click 逻辑,它在需要时等待页面加载。

所以我的问题是:如何在不等待新页面加载的情况下明确执行SelectByValue(或Click)?

更新: 我真正想在我的代码中包含的是:

// the following command should not wait for the new page to load
hereOrThereSelectElement.SelectByValueButDoNotWait("here");
webDriverWait.Until(/* some "page has loaded" check */)

【问题讨论】:

  • 我不确定您是否可以执行 Click() 并且不选择等待页面加载。请查看以下代码是否满足您的需要。 public const int LongRunningTestWait = 60; var webDriverWait = new WebDriverWait(Driver, TimeSpan.FromSeconds(LongRunningTestWait)); try { hereOrThereSelectElement.SelectByValue("here"); webDriverWait.Until(arg =&gt; IsElementPresent(elementInNewPage)); } catch (Exception e) { // Throw exception }
  • 由于SelectByValue 已经在等待新页面加载,因此无需后续调用webDriverWait.Until。但这正是我想要工作的代码。

标签: c# webdriver selenium-webdriver


【解决方案1】:

最后,我找到了解决办法。

与此同时,问题已经稍微转移到了对超时的 WebElement 的 Click() 方法的调用。尽管如此,以下解决方案也适用于调用SelectByValue() 的原始问题。

因此,可以使用 Selenium 的 Actions 低级交互构建器来执行对元素的点击。

var actions = new Actions(webDriver);
actions
  .MoveToElement(aClickableWebElement);
  .Click();
  .Build()
  .Perform();

也许这会对遇到类似问题的人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 2019-06-11
    • 1970-01-01
    相关资源
    最近更新 更多