pythonselenium-webdriverproxybrowser-automationundetected-chromedriver

Cannot reach the site when using selenium with proxy


When i try to access a random website via undetected-chromedriver with proxy configured. Using the code below:

from time import sleep
import json
import undetected_chromedriver as webdriver
from selenium.webdriver.chromium.service import ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options

service = ChromiumService(executable_path=ChromeDriverManager().install())

config = json.load(open('config.json','r'))

def checker(proxy):

    opts = Options()
    opts.binary_location = config['chrome-path']
    opts.add_argument("--proxy-server=" + "\"" + proxy + "\"")

    driver = webdriver.Chrome(version_main=110, options=opts, service=service)
    sleep(5)

    driver.get('https://whatismyip.com')
    sleep(100)

checker('IP:PORT')

The config.json have nothing special:

{
    "chrome-path": "/bin/ungoogled-chromium"
}

The result is that my browser cannot reach any given website. enter image description here

When launching chromium manually via commandline ungoogled-chromium --proxy-server="IP:PORT" (with the same ip of course) all the site load as normal. I also try seleniumwire but it also not work. I hope to find any possible cause and way to fix. Thanks!!


Solution

  • You can use SeleniumBase to proxy to a site while using undetected-chromedriver mode.

    pip install seleniumbase, then run this with python:

    from seleniumbase import SB
    
    with SB(uc=True, proxy="IP:PORT") as sb:
        sb.driver.get("https://whatismyip.com")
        sb.sleep(10)