Skip to content

Discover how to make your web scraping more effective by using Selenium together with Undetected ChromeDriver.

Notifications You must be signed in to change notification settings

IPRoyal/python-undetected-chromedriver

Repository files navigation

How to Use Undetected ChromeDriver in Python Selenium

Many websites today detect and block automated browsing with regular Selenium. The undetected-chromedriver helps bypass these systems by mimicking real browser behavior and automatically evading detection.

Installation and Setup

You can install undetected-chromedriver with pip, and you’re ready to go - Selenium comes bundled, so there’s no need to install it separately.

pip install undetected-chromedriver

Sending a GET Request

GET requests are the core of web scraping and with Undetected ChromeDriver you open pages the same way as with Selenium. This function starts an undetected Chrome instance, navigates to the URL, and waits for you to press Enter before closing.

import undetected_chromedriver as uc

def open_webpage(url):
    # Create a Chrome browser instance with undetected-chromedriver
    driver = uc.Chrome()

    # Open the specified URL
    driver.get(url)

    # Optionally, you can pause the execution to see the browser or perform actions
    input("Press Enter to continue...")

    # Close the browser
    driver.quit()

# Example usage
open_webpage('https://books.toscrape.com/')

Storing Website Content

When scraping, you’ll often need the full HTML of a page, then pass it to a parser like BeautifulSoup to extract data.

import undetected_chromedriver as uc

def open_webpage(url):
    # Create a Chrome browser instance with undetected-chromedriver
    driver = uc.Chrome()

    # Open the specified URL
    driver.get(url)

    # Retrieve the HTML content of the page
    html_content = driver.page_source

    # Optionally, pause the execution to see the browser or perform other actions
    input("Press Enter to continue...")

    # Close the browser
    driver.quit()

    # Return the HTML content if needed outside this function
    return html_content

# Example usage
html_data = open_webpage('https://books.toscrape.com/')
print(html_data)

Changing Undetected ChromeDriver Settings

By default, Undetected ChromeDriver runs with stealth-optimized settings, which usually work well. Still, you may want to tweak options for performance or to bypass stricter anti-bot systems.

Headless mode

Example sets headless mode so the browser runs invisibly.

import undetected_chromedriver as uc

def open_webpage(url):
    
    ## Set Chrome Options
    options = uc.ChromeOptions()
    
    ## Switch Undetected ChromeDriver to Headless Mode
    options.add_argument('--headlessmode')
    
    # Create a Chrome browser instance with undetected-chromedriver
    driver = uc.Chrome(options=options)

    # Open the specified URL
    driver.get(url)

    # Retrieve the HTML content of the page
    html_content = driver.page_source

    # Optionally, pause the execution to see the browser or perform other actions
    input("Press Enter to continue...")

    # Close the browser
    driver.quit()

    # Return the HTML content if needed outside this function
    return html_content

# Example usage
html_data = open_webpage('https://books.toscrape.com/')
print(html_data)

Custom user-agent

We can set a custom user-agent and print it with navigator.userAgent to confirm it’s applied correctly. Other options include choosing a common Chrome version or using a custom driver binary if you’ve built one.

import undetected_chromedriver as uc

def open_webpage(url):

    ## Set Chrome Options
    options = uc.ChromeOptions()

    ## Switch Undetected ChromeDriver to Headless Mode
    options.add_argument('--headlessmode')
    options.add_argument('--user-agent=Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36')

    # Create a Chrome browser instance with undetected-chromedriver
    driver = uc.Chrome(options=options)
    
    current_user_agent = driver.execute_script("return navigator.userAgent;")
    print("Current User Agent:", current_user_agent)
    # Open the specified URL
    driver.get(url)

    # Retrieve the HTML content of the page
    html_content = driver.page_source

    # Optionally, pause the execution to see the browser or perform other actions
    input("Press Enter to continue...")

    # Close the browser
    driver.quit()

    # Return the HTML content if needed outside this function
    return html_content

# Example usage
html_data = open_webpage('https://books.toscrape.com/')
print(html_data)

Using proxies

Undetected ChromeDriver can’t bypass every anti-bot system - many blocks detect traffic patterns (e.g., too many requests) rather than the browser itself. Use proxies to spread requests and lower ban risk; uc makes adding proxies as simple as passing a --proxy-server option.

import undetected_chromedriver as uc

def open_webpage(url):
    # Set Chrome Options
    options = uc.ChromeOptions()

    # Switch Undetected ChromeDriver to Headless Mode
    options.add_argument('--headless')  # Correct argument for headless mode
    options.add_argument('--user-agent=Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36')

    # Proxy settings: Specify your proxy address and port
    proxy_address = "http://your.proxy.server:port"
    options.add_argument(f'--proxy-server={proxy_address}')

    # Create a Chrome browser instance with undetected-chromedriver
    driver = uc.Chrome(options=options)
    
    # Fetch the current user agent to verify
    current_user_agent = driver.execute_script("return navigator.userAgent;")
    print("Current User Agent:", current_user_agent)

    # Open the specified URL
    driver.get(url)

    # Retrieve the HTML content of the page
    html_content = driver.page_source

    # Optionally, pause the execution to see the browser or perform other actions
    input("Press Enter to continue...")

    # Close the browser
    driver.quit()

    # Return the HTML content if needed outside this function
    return html_content

# Example usage
html_data = open_webpage('https://books.toscrape.com/')
print(html_data)

Alternatives to Undetected ChromeDriver

Since it’s built on Selenium, alternatives include Puppeteer, Pyppeteer, or Playwright — but these require bigger code changes and sometimes custom Chromium builds.

To avoid bans, tweak your setup: test headless vs headful mode, rotate user-agents and proxies, and adjust request patterns. Even with stealth drivers, sending too many requests or crawling too aggressively will trigger anti-bot defenses.

About

Discover how to make your web scraping more effective by using Selenium together with Undetected ChromeDriver.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages