[Python] Using Proxies with Selenium / add_to_capabilities error?

Discussão em 'Python' iniciado por Stack, Maio 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am using Selenium with proxies a long time with the following code:

    import os
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.common.proxy import Proxy, ProxyType
    from selenium.webdriver.support.ui import WebDriverWait

    print(f"Checking Browser driver...")
    os.environ['WDM_LOG'] = '0'
    options = Options()
    options.add_argument("start-maximized")
    options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    options.add_experimental_option('useAutomationExtension', False)
    options.add_argument('--disable-blink-features=AutomationControlled')
    srv=Service()

    HOSTNAME = "196.247.168.34"
    PORT = "12345"
    prox = Proxy()
    prox.proxy_type = ProxyType.MANUAL
    prox.http_proxy = prox.ssl_proxy = f"{HOSTNAME}:{PORT}"
    capabilities = webdriver.DesiredCapabilities.CHROME
    prox.add_to_capabilities(capabilities)
    driver = webdriver.Chrome (service=srv, options=options, desired_capabilities=capabilities)
    waitWD = WebDriverWait (driver, 5)
    driver.get ("https://whatismyipaddress.com/")
    input("Press!")#


    But now i get this error when running the code:

    (selenium) C:\DEVNEU\Python-Diverses\Selenium>python test.py
    Checking Browser driver...
    Traceback (most recent call last):
    File "C:\DEVNEU\Python-Diverses\Selenium\test.py", line 25, in <module>
    prox.add_to_capabilities(capabilities)
    ^^^^^^^^^^^^^^^^^^^^^^^^
    AttributeError: 'Proxy' object has no attribute 'add_to_capabilities'. Did you mean: 'to_capabilities'?


    Its an authenticated proxy with username and password - so i can´t simply use this in the options

    options.add_argument(f"--proxy-server={proxy}")


    How can i use my authenticated proxy with selenium without using any other additonal libraries like seleniumbase or seleniumwire?

    Continue reading...

Compartilhe esta Página