3

我们的网络环境使用代理服务器连接到外部Internet,在IE => Internet选项=>连接=>局域网设置(如“10.212.20.11:8080”)中配置。如何将chomedriver与selenium webdriver的代理一起使用?

现在,我使用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 => Internet选项=>连接=>局域网设置中,添加异常地址为NOT代理“127.0.0.1”,这个问题就解决了!不管怎么说,还是要谢谢你!

+1

解决!只需在IE => Internet选项=>连接=>局域网设置中,添加异常地址为NOT代理“127.0.0.1”,这个问题就解决了!不管怎么说,还是要谢谢你! –

+0

我在调用'.quit()'时遇到了407错误的类似问题,并且您的修复在我的情况中很有帮助。我已经发布了一个[问题和答案](http://stackoverflow.com/questions/22018126/selenium-chromedriver-http-407-on-quit)突出解决方案,包括一个链接回到这里。谢谢! – Tetrinity

回答

2

它可能使用硒web驱动程序的命令行启动Chrome。为代理的命令行是:

--proxy服务器=:

+0

解决!只需在IE => Internet选项=>连接=>局域网设置中,添加异常地址为NOT代理“127.0.0.1”,这个问题就解决了!不管怎么说,还是要谢谢你! –

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

我将保存从痛苦的人。如果您的代理服务器要求您传递用户名/密码,那么无法直接通过网址本身传递它。

我想让它与Proxymesh一起工作,所以我做了什么,去控制面板和白名单我的机器,所以它不会要求我的电脑的用户名/密码。

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

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") 
相关问题