在已经运行的Selenium WebDriver上设置功能 [英] Set capability on already running selenium webdriver

查看:72
本文介绍了在已经运行的Selenium WebDriver上设置功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在硒测试步骤(如单击按钮)中,我要防止硒等待页面完成加载.我不能抛出load Exception,因为那样我就不能再处理该页面了. 可以做类似这样的事情:

In selenium test step (like a button click) i want to prevent the selenium waiting for page finish loading. I cant throw the load Exception because then i cant work with the page anymore. Its possible to do a simmilar thing like this:

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

我想要的是"dr.setCapability(" pageLoadStrategy," none);"但仅需指定一个步骤.

What I want is like "dr.setCapability("pageLoadStrategy", "none");" but just for one specifique step.

有人知道这样做的方法吗?

Does anyone know a way to do this?

推荐答案

浏览器启动后,功能将不再可编辑. 暂时禁用等待的一种方法是通过脚本注入实现您自己的get.

Capabilities are no longer editable once the browser is launched. One way to temporary disable the waiting is to implement your own get with a script injection.

类似这样的东西:

// 
// 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 WebDriver上设置功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆