ERR_TUNNEL_CONNECTION_FAILED when using proxy in undetected chromedriver

74 Views Asked by At

WHen I set up my proxy this way:

options = uc.ChromeOptions()
options.add_argument(f'--proxy-server={host}:{port}')

I get ERR_TUNNEL_CONNECTION_FAILED error on many websites.

When I folowed advice here to use desired_capabilities to set the proxy I ran into a wall, cause I can't make desired capabilities to work with undetected chromedriver

I tried this:

import random
import time
from selenium.webdriver import DesiredCapabilities
from undetected_chromedriver import Chrome, ChromeOptions

# Read user agents from file
with open("user-agents.txt", "r") as file:
    user_agents = file.readlines()

# Choose a random user agent
random_user_agent = random.choice(user_agents).strip()

# Generate a random number between 0 and 9
random_number = random.randint(0, 9)

# Define proxy settings
proxy_host = "gate.smartproxy.com"
proxy_port = 10000 + random_number
proxy = f"{proxy_host}:{proxy_port}"

# Set up Chrome options
options = ChromeOptions()

# Set user agent
options.add_argument(f"user-agent={random_user_agent}")

capabilities = DesiredCapabilities.CHROME.copy()
capabilities.CHROME['proxy'] = {
    "httpProxy": proxy,
    "ftpProxy": proxy,
    "sslProxy": proxy,
    "proxyType": "MANUAL",

}

# Add proxy configuration to Chrome options
options.add_argument("--disable-blink-features=AutomationControlled")

# Create the Undetected Chrome driver instance with configured options
driver = Chrome(options=options, desired_capabilities=capabilities)

# Navigate to a website to check the IP address
driver.get("https://www.whatismyip.com/")
time.sleep(10)  # Allow time for the page to load

# Keep the browser open for a long time for testing purposes
time.sleep(3000000)

Andyone has solution to this problem ? How can I configure the proxy to work on all websites on undetected chromedriver ?

1

There are 1 best solutions below

0
Michael Mintz On

SeleniumBase has UC Mode with a proxy option for undetected-chromedriver:

After pip install seleniumbase you can run these once you set the proxy details:

Without auth:

from seleniumbase import Driver
driver = Driver(uc=True, proxy="IP:PORT")

With auth:

from seleniumbase import Driver
driver = Driver(uc=True, proxy="USER:PASS@IP:PORT")

Works with standard driver commands, and also adds some new ones.

driver.get("https://www.whatismyip.com/")

For more control over the time that the driver is disconnected from Chrome to prevent detection, you can do something like this:

driver.uc_open_with_reconnect("https://top.gg/", 5)

Most importantly, make sure you use uc=True to activate UC Mode within SeleniumBase. (That mode is not enabled by default.) There are other ways to activate it, such as with the SB(uc=True) context manager. (It's in the SeleniumBase documentation.)