4

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

swor
  • 751
  • 3
  • 8
  • 21
  • Does this answer your question? [how to set proxy with authentication in selenium chromedriver python?](https://stackoverflow.com/questions/55582136/how-to-set-proxy-with-authentication-in-selenium-chromedriver-python). Answer link - https://stackoverflow.com/a/55582859/5372079 – Abhishek Dhoundiyal Jul 30 '21 at 05:22
  • No, because this does not work in headless mode, see comments!!! – swor Jul 30 '21 at 16:33
  • 1
    yes, my bad because headless mode doesn't support extension. Can you try this solution https://stackoverflow.com/a/56276796/5372079 – Abhishek Dhoundiyal Jul 31 '21 at 11:50
  • I can't use seleniumwire, only pure selenium is avaliable – swor Aug 01 '21 at 11:26

1 Answers1

2

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

CatChMeIfUCan
  • 569
  • 1
  • 7
  • 26
  • Why do i need GUI to handle work with proxy? (for example i can use proxy without authentication in HEADLESS mode) – swor Aug 01 '21 at 13:57
  • because there is an alert/prompt that you should interact with... – CatChMeIfUCan Aug 01 '21 at 14:18
  • the easiest way is virtual display but there is another solution which is [mitmproxy](https://github.com/mitmproxy/mitmproxy) which is way harder and complicated that's why I recommended `Xvfb` – CatChMeIfUCan Aug 01 '21 at 14:23
  • 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](https://bugs.chromium.org/p/chromium/issues/detail?id=718235) and [Issue 706008](https://bugs.chromium.org/p/chromium/issues/detail?id=706008#c5) – CatChMeIfUCan Aug 01 '21 at 14:33