Harshit Jindal
Harshit Jindal

Reputation: 621

Permanently blocking a network call / url on IE, Edge, Chrome or Firefox

I have a webpage that I have to work on all day, it's a portal. I need to reload it about 500 times a day, at least.

I find it to be painfully slow, and I have identified that it makes a network call (just to load a profile picture) to a particular website that I cannot list here, and that network call timesout.
Now because it times out, I don't get a profile picture anyway, but it ends up wasting about 30 seconds per reload.

I can block the domain in the "Networks" tab of Chrome Dev Tools, but I am looking for a more permanent fix. I don't want to have the Dev Tools open all the time since it uses precious screen real estate. I haven't found out a way to permanently block that particular network call which will save me hours per day.

There are 3 things that come to my mind right now:

I'm comfortable with a fix on any browser (Internet Explorer, Edge, Chrome, Firefox). Any leads on this one? I can't be the only person to face this, and yet I haven't found out a solution for it without using admin access, which I don't have.

Upvotes: 1

Views: 662

Answers (2)

Harshit Jindal
Harshit Jindal

Reputation: 621

Workaround 2: Launch Chrome Window from Selenium while executing Network.setBlockedURLs. It will only work in Selenium 4 and above, but it's not working as of now on Selenium 4.0.0.b3.

https://chromedevtools.github.io/devtools-protocol/tot/Network/#method-setBlockedURLs

from selenium import webdriver
driver = webdriver.Chrome()
driver.execute_cdp_cmd('Network.setBlockedURLs', {"urls": ["https://www.somelink.com/*"]})
driver.get("www.mywebsite.com")

Upvotes: 1

Harshit Jindal
Harshit Jindal

Reputation: 621

Workaround 1: Call the chrome instance from Selenium, and disable all images. Works, but not the best approach.

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)

Upvotes: 1

Related Questions