【发布时间】: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 => IsElementPresent(elementInNewPage)); } catch (Exception e) { // Throw exception } -
由于
SelectByValue已经在等待新页面加载,因此无需后续调用webDriverWait.Until。但这正是我想要工作的代码。
标签: c# webdriver selenium-webdriver