Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

java - Set capability on already running selenium webdriver

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);

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

Does anyone know a way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Something like this:

// 
// 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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...