6

I started playing with Seleniumlibrary tests (run with robotframework) and as our websites have ads and metrics and so on, every time I run a test those URLs get loaded.

Is there a way to tell selenium/robotframework to not load certain types of URLs or prevent it to load external resources (i.e. everything that is not from localhost).

gforcada
  • 2,498
  • 17
  • 26

2 Answers2

10

You can do that with browsermob-proxy. Once you have that installed, you simply setup the proxy and set the blacklist.

ProxyServer server = new ProxyServer(9000)
server.start();
final DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, server.seleniumProxy());
//Send a 200 for all requests to the facebook cdn
server.blacklistRequests("http://.*\\.fbcdn.net/.*", 200); 
//Finish setting up your driver
WebDriver driver = new SomeDriverImpl(capabilities);

I believe the following will work with this python wrapper (the regex might be slightly different):

from browsermobproxy import Server
server = Server("path/to/browsermob-proxy")
server.start()
proxy = server.create_proxy()
proxy.blacklist('http://.*\\.fbcdn.net/.*', 200)
from selenium import webdriver
profile  = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
...
proxy.stop()
driver.quit()
Scott
  • 9,458
  • 7
  • 54
  • 81
  • You didn't state which language you were using, so I assumed Java. Is this not correct? A quick search shows that there are many wrappers for that library for python. – Scott Mar 12 '13 at 13:24
  • Sorry, yes I'm using python. Thanks for the response, will mark is as valid once I got around to try it! – gforcada Mar 13 '13 at 14:32
  • 2
    Shouldn't you escape the dot in fbcdn.net, for example: `server.blacklistRequests("https?://.*\\.fbcdn\\.net/.*", 200);` – Lordalcol Dec 14 '15 at 11:54
2

This can now be done in two lines in Chrome:

driver.execute_cdp_cmd('Network.setBlockedURLs', {"urls": ["www.baidu.com"]})
driver.execute_cdp_cmd('Network.enable', {})
Retiarius
  • 334
  • 2
  • 12