5

I have to use selenium and proxy with authentication. I have a few constraints

  1. I can't use selenium-wire (only pure selenium allowed)
  2. I have to use headless mode (e.g. chrome_options.add_argument("--headless"))

I read this answer Python proxy authentication through Selenium chromedriver but it doesn't work for headless mode.

Is it possible to use proxy with authentication in my case? (browser(Chrome, Firefox) is not important)


I need python function to create selenium webdriver object authenticate to proxy

4

2 Answers 2

2
+100

you can't because you need a GUI to handle it with selenium in your case so I would recommend using a virtual display like Xvfb display server

You can use PyVirtualDisplay (a Python wrapper for Xvfb) to run headless.

for Linux

sudo apt-get install firefox xvfb

install virtual display for python

pip install pyvirtualdisplay

then

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

# now Firefox will run in a virtual display. 
# you will not see the browser.
browser = webdriver.Firefox()
browser.get('http://www.google.com')
print browser.title
browser.quit()

display.stop()

in this case, you do not need to add options.add_argument("--headless") argument and you can follow the answers commented above as a solution or doing it your way but I think this is the best solution for using pure selenium for proxy

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

4 Comments

Why do i need GUI to handle work with proxy? (for example i can use proxy without authentication in HEADLESS mode)
because there is an alert/prompt that you should interact with...
the easiest way is virtual display but there is another solution which is mitmproxy which is way harder and complicated that's why I recommended Xvfb
in the mitmproxy case, you have to run a separate proxy without authentication and redirect it to the target proxy with authentication, as I said the Xvfb is easier and more logical, by the way, headless selenium itself does not support authentication, See Issue 718235 and Issue 706008
0

It's the future, and there is a simpler solution now! (I'm using Chrome here)

pip install seleniumbase # tested on seleniumbase==4.30.8
import re
from types import SimpleNamespace

from seleniumbase import Driver

proxy_args = SimpleNamespace(
    username="your-proxy-server-username",
    password="your-proxy-server-password",
    port=6969,
    endpoint="your.proxy.server.domain",  # e.g. bright data's is "brd.superproxy.io",
)
proxy_args.auth_url = f"{proxy_args.username}:{proxy_args.password}@{proxy_args.endpoint}:{proxy_args.port}"

for _ in range(3):
    selenium_driver = Driver(uc=True, headless2=True, proxy=proxy_args.auth_url)
    selenium_driver.uc_open_with_reconnect("https://ipinfo.io/ip", 3)
    print(
        "your IP address is ",
        re.search(r"(\d+\.){3}\d+", selenium_driver.page_source).group(),
    )
    selenium_driver.close()

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.