10

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?

t1daz
  • 140
  • 1
  • 13
  • I doubt any driver as of now allow controlling capabilities after that driver is started. So I assume this may not be possible AFAIK – Tarun Lalwani Sep 14 '17 at 16:59

2 Answers2

6

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);
}
Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • thanks for your answer. if i'm not wrong but with that script the page will stop loading.. and it's not wat I want. The page continue loading but i want selenium not waiting for that in a specific step. – t1daz Sep 15 '17 at 09:57
3

As of the current implementation of Selenium once we configure the WebDriver instance with our intended configuration through DesiredCapabilities class and initialize the WebDriver session to open a Browser, we cannot change the capabilities runtime.

It is worth to mention, somehow if you are able to retrieve the runtime capabilities still you won't be able to change them back.

So, in-order to make a change in the pageLoadStrategy you have to initiate a new WebDriver session.

Here is @JimEvans clear and concise answer (as of Oct 24 '13 at 13:02) related to proxy settings capability:

When you set a proxy for any given driver, it is set only at the time WebDriver session is created; it cannot be changed at runtime. Even if you get the capabilities of the created session, you won't be able to change it. So the answer is, no, you must start a new session if you want to use different proxy settings.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • thanks, i see that is not possible but creating a new webDriver is not a good option for me also because i cannot point that driver to the old – t1daz Sep 15 '17 at 10:00
  • 1
    Is this still how it works? I have seen some Javascript workarounds but a native solution would be nicer. – d-b Dec 12 '20 at 23:48