     [Answers](https://scrapfly.io/blog)   /  [selenium](https://scrapfly.io/blog/tag/selenium)   /  [How to block resources in Selenium and Python?](https://scrapfly.io/blog/answers/how-to-block-resources-in-selenium)   # How to block resources in Selenium and Python?

 by [Bernardas Alisauskas](https://scrapfly.io/blog/author/bernardas) Jun 30, 2023 2 min read [\#selenium](https://scrapfly.io/blog/tag/selenium) 

 [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-block-resources-in-selenium "Share on LinkedIn")    

 

 

To speed up Selenium web scrapers we can block media and other non-essential background requests.

Unfortunately, Selenium by itself doesn't support request interception and blocking so we must use a proxy to handle the blocking for us, then attach this proxy to our Selenium instance.

For example, a popular proxy for such use case is [mitproxy](https://pypi.org/project/mitmproxy/). We can easily configure it to block requests by resource type or by resource name.

First install mitmproxy using `pip install mitmproxy` or package manager available in your operating system. Then, we can create a simple `block.py` script that will extend mitmproxy with our custom blocking logic:

python```python
# block.py
from mitmproxy import http

# we can block popular 3rd party resources like tracking and advertisements.
BLOCK_RESOURCE_NAMES = [
  'adzerk',
  'analytics',
  'cdn.api.twitter',
  'doubleclick',
  'exelator',
  'facebook',
  'fontawesome',
  'google',
  'google-analytics',
  'googletagmanager',
  # or something abstract like images
  'images'
]
# or block based on resource extension
BLOCK_RESOURCE_EXTENSIONS = [
    '.gif',
    '.jpg',
    '.jpeg',
    '.png',
    '.webp',
]

# this will handle all requests going through proxy:
def request(flow: http.HTTPFlow) -> None:
    url = flow.request.pretty_url
    has_blocked_extension = any(url.endswith(ext) for ext in BLOCK_RESOURCE_EXTENSIONS)
    contains_blocked_key = any(block in url for block in BLOCK_RESOURCE_NAMES)
    if has_blocked_extension or contains_blocked_key:
        print(f"Blocked {url}")
        flow.response = http.Response.make(
            404,  # status code
            b"Blocked",  # content
            {"Content-Type": "text/html"}  # headers
        )
```



We can run this proxy using `mitmproxy -s block.py` and it'll start a proxy on `localhost:8080` on our machine.

Now, we can attach this proxy to our Selenium instance and it'll block all unwanted requests going through it:

python```python
from selenium import webdriver

PROXY = "localhost:8080"  # IP:PORT or HOST:PORT of our mitmproxy

chrome_options = webdriver.ChromeOptions()
# this command enabled proxy for our Selenium browser:
chrome_options.add_argument('--proxy-server=%s' % PROXY)

chrome = webdriver.Chrome(options=chrome_options)
# test it by going to a page with blocked resources:
chrome.get("https://web-scraping.dev/product/1")
chrome.quit()
```



Using this method to block resources can significantly reduce the bandwidth used by the Selenium scraper - often by 2-10 times! This will also greatly speed up scraping as the browser doesn't need to render unnecessary resources.

🤖 Tip: to use `mitmproxy` with Selenium and `https` websites the mitmproxy certificate needs to be installed for that see [how to install mitmproxy certificate](https://scrapfly.io/blog/answers/how-to-install-mitmproxy-certificate)



 

    



Scale Your Web Scraping

Anti-bot bypass, browser rendering, and rotating proxies, all in one API. Start with 1,000 free credits.

  No credit card required  1,000 free API credits  Anti-bot bypass included 

 [Start Free](https://scrapfly.io/register) [View Docs](https://scrapfly.io/docs/onboarding) 

 Not ready? Get our newsletter instead. 

 

## Explore this Article with AI

 [ ChatGPT ](https://chat.openai.com/?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-block-resources-in-selenium) [ Gemini ](https://www.google.com/search?udm=50&aep=11&q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-block-resources-in-selenium) [ Grok ](https://x.com/i/grok?text=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-block-resources-in-selenium) [ Perplexity ](https://www.perplexity.ai/search/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-block-resources-in-selenium) [ Claude ](https://claude.ai/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-block-resources-in-selenium) 



 ## Related Articles

 [  

 python selenium 

### Intro to Web Scraping Using Selenium Grid

In this guide, you will learn about installing and configuring Selenium Grid with Docker and how to use it for web scrap...

 

 ](https://scrapfly.io/blog/posts/intro-to-web-scraping-using-selenium-grid) [     

### What is HTTP 405 Error? (Method Not Allowed)

HTTP error codes can be confusing, especially when they disrupt your web scraping or automation tasks. One such error is...

 

 ](https://scrapfly.io/blog/posts/what-is-http-405-error) [  

 http 

### What is HTTP 409 Error? (Conflict)

HTTP status code 409 generally means a conflict or mismatch with the server state. Learn why it happens and how to avoid...

 

 ](https://scrapfly.io/blog/posts/what-is-http-409-status-code-conflict) 

  ## Related Questions

- [ Q How to install mitmproxy certificate on Chrome and Chromium? ](https://scrapfly.io/blog/answers/how-to-install-mitmproxy-certificate)
- [ Q How to capture background requests and responses in Selenium? ](https://scrapfly.io/blog/answers/how-to-capture-xhr-requests-selenium)
- [ Q How to block resources in Playwright and Python? ](https://scrapfly.io/blog/answers/how-to-block-resources-in-playwright)
- [ Q How to block resources in Puppeteer? ](https://scrapfly.io/blog/answers/how-to-block-resources-in-puppeteer)
 
  



   



 Run headless browsers at scale, **1,000 free credits** [Start Free](https://scrapfly.io/register)