[Python] Selenium + 2Captcha Proxy Not Working for Website

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    I’m trying to use a paid 2Captcha proxy with Selenium to access Finland insurance site. The proxy works with requests.get(), but when I try to access the website using Selenium, it fails.

    I’ve verified the proxy manually, and it works. I’ve tried adding --proxy-server to ChromeOptions. No clear error messages appear, the page just doesn’t load.

    What am I missing?

    Here is what I get when the page opens (sorry it is in Croatian):

    [​IMG]

    Here’s a minimal reproducible example of the code:

    import requests
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import time

    # Proxy credentials
    USERNAME = "your_username"
    PASSWORD = "your_password"
    PROXY_DNS = "your_proxy_ip:port"


    def test_proxy():
    """Test if the proxy is working."""
    proxy_url = f"http://{USERNAME}:{PASSWORD}@{PROXY_DNS}"
    proxies = {"http": proxy_url, "https": proxy_url}

    try:
    response = requests.get("http://ip-api.com/json", proxies=proxies, timeout=10)
    print("Proxy Response:", response.json())
    except requests.RequestException as e:
    print("Proxy failed:", e)


    def setup_driver():
    """Setup Selenium WebDriver with proxy."""
    chrome_options = webdriver.ChromeOptions()
    proxy = f"http://{USERNAME}:{PASSWORD}@{PROXY_DNS}"
    chrome_options.add_argument(f"--proxy-server={proxy}")

    driver = webdriver.Chrome(options=chrome_options)
    return driver


    def test_website():
    """Test if Selenium can access the website."""
    driver = setup_driver()
    driver.get("https://www.if.fi/henkiloasiakkaat/vakuutukset/autovakuutus")

    try:
    WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.TAG_NAME, "body"))
    )
    time.sleep(10)
    print("Page loaded successfully.")
    except:
    print("Failed to load the page.")

    driver.quit()


    if __name__ == "__main__":
    test_proxy() # Check if proxy works via requests
    test_website() # Check if proxy works with Selenium

    Continue reading...

Compartilhe esta Página