Can I automate Chrome request blocking using Selenium-webdriver for Ruby? Can I automate Chrome request blocking using Selenium-webdriver for Ruby? selenium selenium

Can I automate Chrome request blocking using Selenium-webdriver for Ruby?


To block URLs from loading with Selenium with the DevTool API:

def send_cmd(driver, cmd, params={})  bridge = driver.send(:bridge)  resource = "session/#{bridge.session_id}/chromium/send_command_and_get_result"  response = bridge.http.call(:post, resource, {'cmd':cmd, 'params': params})  raise response[:value] if response[:status]  return response[:value]endsend_cmd(driver, "Network.setBlockedURLs", {'urls': ["*"]})send_cmd(driver, "Network.enable")


It's not very well documented, but you can also implement request blocking by passing the host-resolver-rules option to chrome and mapping the domain to localhost or an invalid IP. Something like this should work for you:

options = Selenium::WebDriver::Chrome::Options.newoptions.add_argument('--host-resolver-rules=MAP www.google-analytics.com 127.0.0.1')driver = Selenium::WebDriver.for :chrome, options: options


Try https://github.com/lightbody/browsermob-proxy

I dont know if it can satisfy your requirement as I am no way a network expert, I do use browsermob-proxy extensively to capture network request along with selenium and there is a method to blacklist certain request

https://browsermob-proxy-py.readthedocs.io/en/stable/client.html#browsermobproxy.Client.blacklist

How to disable loading external urls on seleniumlibrary/robotframework