1

My simple selenium python script to test how proxy fails. How to force webdriver work through proxy connection?

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

PROXY_HOST = "{MYIP}"
PROXY_PORT = 11200
PROXY_USERNAME = "{UNAME}"
PROXY_PASSWORD = "{PW}"

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=http://%s:%s@%s:%s' % (PROXY_USERNAME, PROXY_PASSWORD, PROXY_HOST, PROXY_PORT))
chrome_options.add_argument("ignore-certificate-errors")

chrome = webdriver.Chrome(options=chrome_options)
chrome.get("https://www.ipchicken.com/")

# Wait until a table is present
wait = WebDriverWait(chrome, 30)  # Adjust the timeout as needed
wait.until(EC.presence_of_element_located((By.TAG_NAME, "table")))

# Get the page source
page_source = chrome.page_source

# Output page data as text in console
print(page_source)

# Close the browser
chrome.quit()

It works good if i remove Proxy settings, but, it throws TimeoutException with proxy settings.

My proxy credentials are correct and work well through CURL.

3
  • we cant test your credentials or how did you check proxy work Commented May 13, 2024 at 11:29
  • i'm using iproyal proxy. I used CURL request: curl -v -x USER:[email protected]:12321 -L ipv4.icanhazip.com And it returned changed IP Commented May 13, 2024 at 11:53
  • then try to remove http:// on chrome_options.add_argument('--proxy-server= but i think you can set proxy auth there. I solved using whitelist in my proxy provider or with a proxy server who call the real proxy and use proxy server on selenium Commented May 13, 2024 at 11:55

1 Answer 1

0

You've got 4 options for authenticated proxy with Selenium Python:

  1. Load a Chrome extension in your Python Selenium script to set those proxy settings: https://stackoverflow.com/a/35293284/7058266

  2. Use selenium-wire: https://stackoverflow.com/a/56276796/7058266

  3. Use seleniumbase: https://stackoverflow.com/a/77580114/7058266

  4. Combine selenium-wire with seleniumbase and change the proxy in the middle of a script as often as you'd like: https://stackoverflow.com/a/78188999/7058266

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.