如何使用chromedriver与代理selenium webdriver?

时间:2013-06-19 03:39:49

标签: python google-chrome selenium webdriver selenium-chromedriver

我们的网络环境使用代理服务器连接到外部互联网,在IE =>中配置互联网选项=> Connections =>局域网设置,如“10.212.20.11:8080”。

现在,我正在使用selenium webdriver for chrome和IE,但启用了代理服务器,我无法启动浏览器。

这是python代码:

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

这是错误消息(如果在IE“Internet选项”中禁用代理,它可以正常工作):

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

那么,如何设置chromedriver的代理? (IE驱动程序有同样的问题)。

谢谢Ehsan,但我更改了代码,错误仍然存​​在。

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()

解决!只是在IE =&gt;互联网选项=&gt; Connections =&gt;局域网设置,为NOT使用代理“127.0.0.1”添加异常地址,这个问题解决了!不管怎样,谢谢!

4 个答案:

答案 0 :(得分:3)

使用selenium web驱动程序可以使用命令行启动Chrome。代理的命令行是:

- 代理服务器=:

答案 1 :(得分:1)

我会拯救某人免于痛苦。如果您的代理服务器要求您传递用户名/密码,那么它无法直接通过网址传递。

我想让它与Proxymesh一起工作,所以我做了什么,去了控制面板并将我的机器列入白名单,因此我的计算机不需要用户名/ pw。

更多@ https://github.com/webdriverio/webdriverio/issues/324

答案 2 :(得分:0)

它为我工作......

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 :(得分:-1)

这对我有用。 拜托,你可以试试。

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")