How to use chomedriver with a proxy for selenium w

2019-04-11 15:05发布

Our network environment using a proxy server to connect to the outside internet, configured in IE => Internet Options => Connections => LAN Settings, like "10.212.20.11:8080".

Now, I'm using selenium webdriver for chrome and IE, but with the proxy server enabled, I can't start the browser.

Here is the python code:

from selenium import webdriver
driver = webdriver.Chrome(executable_path='E:\Selenium\WebDrivers\chromedriver.exe')

Here is the error message(If disable the proxy in IE "Internet Options", it works fine):

Traceback (most recent call last):
  File "E:\WorkSpace\GitHub\selenium\sandbox\test.py", line 4, in <module>
    driver = webdriver.Chrome(executable_path='E:\Selenium\WebDrivers\chromedriver.exe')
  File "C:\Python27\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 66, in __init__
    self.quit()
  File "C:\Python27\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in quit
    self.service.stop()
  File "C:\Python27\lib\site-packages\selenium\webdriver\chrome\service.py", line 97, in stop
    url_request.urlopen("http://127.0.0.1:%d/shutdown" % self.port)
  File "C:\Python27\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 406, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 519, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 438, in error
    result = self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 625, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Python27\lib\urllib2.py", line 406, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 519, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 444, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 527, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized

So, how to set the proxy for the chromedriver? (IE Driver have the same problem).

Thanks Ehsan, but I changed the code, the error still exists.

from selenium import webdriver

chrome_option = webdriver.ChromeOptions()
chrome_option.add_argument("--proxy-server=10.213.20.62:80" )

driver = webdriver.Chrome(executable_path='E:\Selenium\WebDrivers\chromedriver.exe',
                          chrome_options=chrome_option)

driver.quit()

Solved! Just in IE => Internet Options => Connections => LAN Settings, add exception address for NOT use proxy "127.0.0.1", this problem solved! Thanks anyway!

4条回答
聊天终结者
2楼-- · 2019-04-11 15:33

This is working for me. please you can try.

from selenium import webdriver

PROXY = "23.23.23.23:3128" # IP:PORT or HOST:PORT

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=http://%s' % PROXY)

chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://whatismyipaddress.com")
查看更多
不美不萌又怎样
3楼-- · 2019-04-11 15:36

Its possible for Chrome to be started with command lines with selenium web driver. The command line for the proxy is:

--proxy-server=:

查看更多
甜甜的少女心
4楼-- · 2019-04-11 15:50

I'll save someone from the pain. If you have proxy server that requires you to pass username/pw then it's not possible to pass it through the url itself directly.

I wanted to get it work with Proxymesh so what I did, went to control panel and whitelisted my machine so it wouldn't require username/pw for my computer.

more @ https://github.com/webdriverio/webdriverio/issues/324

查看更多
虎瘦雄心在
5楼-- · 2019-04-11 15:51

Its working for me...

from selenium import webdriver

PROXY = "23.23.23.23:3128" # IP:PORT or HOST:PORT

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=http://%s' % PROXY)

chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://whatismyipaddress.com")
查看更多
登录 后发表回答