I have a program currently working properly and blocking requests with selenium chrome driver with these lines of code:
driver.execute_cdp_cmd('Network.setBlockedURLs', {"urls": urls})
driver.execute_cdp_cmd('Network.enable', {})
But when I run the same code with selenium remote web driver I get the following error
AttributeError: 'WebDriver' object has no attribute 'execute_cdp_cmd'
Found this solution to run the execute_cdp_cmd command code and implemented it in this way but nothing really happens so my question is. Is there any way to block requests in the selenium remote driver?
def send(driver, cmd, params={}):
resource = "/session/%s/chromium/send_command_and_get_result" % driver.session_id
url = driver.command_executor._url + resource
body = json.dumps({'cmd': cmd, 'params': params})
response = driver.command_executor._request('POST', url, body)
if response['status']:
raise Exception(response.get('value'))
return response.get('value')
def add_script(driver):
# Read blocked URL's
with open("blockedURLs.txt") as file:
urls = file.readlines()
urls = [line.rstrip() for line in urls]
send(driver, 'Network.setBlockedURLs', {"urls": urls})
send('Network.enable', {})
WebDriver.add_script = add_script
driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', options=chrome_options)