【问题标题】:Set capability on already running selenium webdriver在已经运行的 selenium webdriver 上设置功能
【发布时间】:2017-09-14 16:38:01
【问题描述】:

在 selenium 测试步骤(如单击按钮)中,我想防止 selenium 等待页面完成加载。我不能抛出加载异常,因为这样我就不能再使用该页面了。 可以做类似这样的事情:

DesiredCapabilities dr = DesiredCapabilities.chrome();
dr.setCapability("pageLoadStrategy", "none");
WebDriver driver = new RemoteWebDriver(new URL("...."), dr);

我想要的是“dr.setCapability("pageLoadStrategy", "none");"但只是为了一个特定的步骤。

有人知道怎么做吗?

【问题讨论】:

  • 我怀疑任何驱动程序在该驱动程序启动后允许控制功能。所以我认为这可能是不可能的 AFAIK

标签: java selenium


【解决方案1】:

一旦浏览器启动,功能就不再可编辑。 暂时禁用等待的一种方法是使用脚本注入实现您自己的get

类似这样的:

// 
// loads the page and stops the loading without exception after 2 sec if 
// the page is still loading.
//

load(driver, "https://httpbin.org/delay/10", 2000); 
public static void load(WebDriver driver, String url, int timeout) {
  ((JavascriptExecutor)driver).executeScript(
    "var url = arguments[0], timeout = arguments[1];"
    "window.setTimeout(function(){window.location.href = url}, 1);" +
    "var timer = window.setTimeout(window.stop, timeout);" +
    "window.onload = function(){window.clearTimeout(timer)}; "
    , url, timeout);
}

【讨论】:

  • 感谢您的回答。如果我没记错,但是使用该脚本,页面将停止加载..这不是我想要的。页面继续加载,但我希望 selenium 不在特定步骤中等待。
【解决方案2】:

一旦我们通过 DesiredCapabilities 类使用我们的预期配置配置 WebDriver 实例并初始化 WebDriver 会话打开浏览器,我们无法更改功能运行时。

值得一提的是,如果您能够检索运行时功能,您仍然无法将它们改回来。

因此,为了对 pageLoadStrategy 进行更改,您必须启动一个新的 WebDriver 会话。

这里是与代理设置功能相关的@JimEvans clear and concise answer(截至 2013 年 10 月 24 日 13:02):

当您为任何给定驱动程序设置代理时,它仅在创建 WebDriver 会话时设置;它不能在运行时更改。即使您获得了已创建会话的功能,您也无法更改它。所以答案是,不,如果你想使用不同的代理设置,你必须开始一个新的会话。

【讨论】:

  • 谢谢,我知道这是不可能的,但是创建一个新的 webDriver 对我来说不是一个好选择,因为我不能将该驱动程序指向旧的
  • 这还是它的工作原理吗?我已经看到了一些 Javascript 解决方法,但原生解决方案会更好。
猜你喜欢
  • 2013-06-27
  • 2017-06-13
  • 2020-02-17
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-19
相关资源
最近更新 更多