如何使用与selenium webdriver代理chomedriver? [英] How to use chomedriver with a proxy for selenium webdriver?
问题描述
现在,我将selenium webdriver用于Chrome和IE,但启用代理服务器后,我无法启动浏览器。
以下是python代码:
from selenium import webdriver
driver = webdriver.Chrome(executable_path ='E:\Selenium\WebDrivers\chromedriver.exe')
这里是错误信息(如果在IE的Internet选项中禁用代理,它可以正常工作):
Traceback(最近一次调用的最后一个):
在< module>文件中,第4行的文件E:\WorkSpace\GitHub\selenium\sandbox\test.py
driver = webdriver.Chrome(executable_path ='E:\Selenium\WebDrivers\chromedriver.exe')
文件C:\Python27\lib\site-packages\selenium \webdriver\chrome\webdriver.py,第66行,在__init__
self.quit()
文件C:\Python27\lib\site-packages\selenium\\ \\ webdriver \ chrome \webdriver.py,第81行,退出
self.service.stop()
文件C:\Python27\lib\site-packages\selenium \webdriver\chrome\service.py,第97行,停止
url_request.urlopen(http://127.0.0.1:%d/shutdown%self.port)
文件C:\Python27\lib\urllib2.py,第126行,在urlopen
中返回_opener.open(url,data,timeout)
文件C:\Python27\lib \urllib2.py,第406行,打开
response = meth(req,response)
在http_response中的文件C:\Python27\lib\urllib2.py,第519行
'http',请求,响应,代码,味精,hdrs)
文件C:\Python27\lib\urllib2.py,第438行,错误
result = self._call_chain(* args)
文件C:\Python27\lib\urllib2.py,行378,在_call_chain
result = func(* args)
文件C:\Python27\lib\\ \\ urllib2.py,第625行,在http_error_302
中返回self.parent.open(new,timeout = req.timeout)
文件C:\Python27\lib\urllib2.py第406行,打开
response = meth(req,response)
在http_response
'http中的文件C:\Python27\lib\urllib2.py,第519行',请求,响应,代码,味精,hdrs)
文件C:\Python27\lib\urllib2.py,第444行,错误
返回self._call_chain(* args)
文件C:\Python27\lib\urllib2.py,行378,在_call_chain
result = func(* args)
文件C:\Python27\ lib_\\urllib2.py,第527行,在http_error_default
中引发HTTPError (req.get_full_url(),code,msg,hdrs,fp)
urllib2.HTTPError:HTTP错误401:未授权
那么,如何设置chromedriver的代理? (IE驱动程序有同样的问题)。
感谢Ehsan,但我改变了代码,错误依然存在。
来自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,这个问题就解决了!无论如何,感谢!
使用selenium web驱动程序可以使用命令行启动Chrome。代理的命令行是:
- proxy-server =:
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!
Its possible for Chrome to be started with command lines with selenium web driver. The command line for the proxy is:
--proxy-server=:
这篇关于如何使用与selenium webdriver代理chomedriver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!