Home > Back-end >  can I block some network connections in selenium chromedriver for java?
can I block some network connections in selenium chromedriver for java?

Time:11-06

If I use chrome dev tools I can block some requests in the current page using the "Network request blocking" feature. see image Network request blocking

can I achieve the same using selenium chhromedriver to block some items in the webpage, for example if I have my java code

WebDriver driver = new ChromeDriver(service, options);
driver.get(scan.getUrl());

the idea here is download the page faster by blocking uninteresting things for me.

CodePudding user response:

The API for DevTools is available in Selenium 4. For example enabling request blocking can be achieved like this:

    ChromeDriver driver = new ChromeDriver();
    driver.getDevTools().createSession();
    driver.getDevTools().send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
    driver.getDevTools().send(Network.setBlockedURLs(Arrays.asList(
            "https://foo/*",
            "https://bar/*",
            "https://baz/*")));
  • Related