0

我正在尝试使用 Python 在 Selenium 中测试一个样本。我正在使用代理服务器通过身份验证连接到 Internet。当我尝试运行以下代码时:

from selenium import webdriver   

if __name__ == '__main__':

    proxy = "some_IP"
    port = 3128

    fp = webdriver.FirefoxProfile()
    fp.set_preference('network.proxy.ssl_port', int(port))
    fp.set_preference('network.proxy.ssl', proxy)
    fp.set_preference('network.proxy.http_port', int(port))
    fp.set_preference('network.proxy.http', proxy)
    fp.set_preference('network.proxy.ftp', proxy)
    fp.set_preference('network.proxy.ftp_port', int(port))
    fp.set_preference('network.proxy.socks', proxy)
    fp.set_preference('network.proxy.socks_port', int(port))

    fp.set_preference('network.proxy.type', 1)

    browser = webdriver.Firefox(firefox_profile=fp)
    browser.set_page_load_timeout(15)
    browser.get('http://www.google.com')
    print browser.title

Firefox 浏览器打开没有任何问题,并且在其代理配置中一切正常,甚至打开了身份验证的弹出窗口。如果我验证自己,我可以毫无问题地导航。问题是在这背后我得到以下错误:

Traceback (most recent call last):
  File "D:/_Vkt0r/iStuffs/Jobs/Projects/test-proxy/test.py", line 25, in <module>
        browser = webdriver.Firefox(firefox_profile=fp)
  File "D:\_Vkt0r\iStuffs\Jobs\Projects\test-proxy\selenium\webdriver\firefox
        \webdriver.py", line 62, in __init__ desired_capabilities=capabilities)     
  File "D:\_Vkt0r\iStuffs\Jobs\Projects\test-proxy\selenium\webdriver\remote
        \webdriver.py", line 72, in __init__ self.start_session(desired_capabilities, 
        browser_profile)      
  File "D:\_Vkt0r\iStuffs\Jobs\Projects\test-proxy\selenium\webdriver\remote
        \webdriver.py", line 114, in start_session 'desiredCapabilities': 
        desired_capabilities,            
  File "D:\_Vkt0r\iStuffs\Jobs\Projects\test-proxy\selenium\webdriver\remote
        \webdriver.py", line 165, in execute
         self.error_handler.check_response(response)
  File "D:\_Vkt0r\iStuffs\Jobs\Projects\test-proxy\selenium\webdriver\remote
        \errorhandler.py", line 136, in check_response raise exception_class(value)
  selenium.common.exceptions.WebDriverException: Message: '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>ERROR: Acceso Denegado a la Cach\xc3\xa9</title> <style type="text/css"><!--  /*\n Stylesheet for Squid Error pages\n Adapted from design by Free CSS Templates\n http://www.freecsstemplates.org\n Released for free under a Creative Commons Attribution 2.5 License\n*/\n\n/* Page basics */\n* {\n\tfont-family: verdana, sans-serif;\n}\n\nhtml body {\n\tmargin: 0;\n\tpadding: 0;\n\tbackground: #efefef;\n\tfont-size: 12px;\n\tcolor: #1e1e1e;\n}\n\n/* Page displayed title area */\n#titles {\n\tmargin-left: 15px;\n\tpadding: 10px;\n\tpadding-left: 100px;\n\tbackground: url(\'http://www.squid-cache.org/Artwork/SN.png\') no-repeat left;\n}\n\n/* initial title */\n#titles h1 {\n\tcolor: #000000;\n}\n#titles h2 {\n\tcolor: #000000;\n}\n\n/* special event: FTP success page titles */\n#titles ftpsuccess {\n\tbackground-color:#00ff00;\n\twidth:100%;\n}\n\n/* Page displayed body content area */\n#content {\n\tpadding: 10px;\n\tbackground: #ffffff;\n}\n\n/* General text */\np {\n}\n\n/* error brief description */\n#error p {\n}\n\n/* some data which may have caused the problem */\n#data {\n}\n\n/* the error message received from the system or other software */\n#sysmsg {\n}\n\npre {\n    font-family:sans-serif;\n}\n\n/* special event: FTP / Gopher directory listing */\n#dirmsg {\n    font-family: courier;\n    color: black;\n    font-size: 10pt;\n}\n#dirlisting {\n    margin-left: 2%;\n    margin-right: 2%;\n}\n#dirlisting tr.entry td.icon,td.filename,td.size,td.date {\n    border-bottom: groove;\n}\n#dirlisting td.size {\n    width: 50px;\n    text-align: right;\n    padding-right: 5px;\n}\n\n/* horizontal lines */\nhr {\n\tmargin: 0;\n}\n\n/* page displayed footer area */\n#footer {\n\tfont-size: 9px;\n\tpadding-left: 10px;\n}\n  body :lang(fa) { direction: rtl; font-size: 100%; font-family: Tahoma, Roya, sans-serif; float: right; } :lang(he) { direction: rtl; }  --></style> </head><body id=ERR_CACHE_ACCESS_DENIED> <div id="titles"> <h1>ERROR</h1> <h2>Cache Acceso Denegado</h2> </div> <hr>  <div id="content"> <p>Se encontr\xc3\xb3 el siguiente error al intentar recuperar la direcci\xc3\xb3n URL: <a href="http://127.0.0.1:12233/hub/session">http://127.0.0.1:12233/hub/session</a></p>  <blockquote id="error"> <p><b>Acceso Denegado a la Cach\xc3\xa9</b></p> </blockquote>  <p>Lo lamento, tu no est\xc3\xa1s autorizado a solicitar http://127.0.0.1:12233/hub/session de este cach\xc3\xa9 hasta que te hayas autenticado.</p>  <p>Please contact the <a href="mailto:webmaster?subject=CacheErrorInfo%20-%20ERR_CACHE_ACCESS_DENIED&body=CacheHost%3A%20squid.proxy%0D%0AErrPage%3A%20ERR_CACHE_ACCESS_DENIED%0D%0AErr%3A%20%5Bnone%5D%0D%0ATimeStamp%3A%20Tue,%2005%20Nov%202013%2019%3A44%3A22%20GMT%0D%0A%0D%0AClientIP%3A%20192.168.10.33%0D%0A%0D%0AHTTP%20Request%3A%0D%0APOST%20%2Fhub%2Fsession%20HTTP%2F1.1%0AAccept-Encoding%3A%20identity%0D%0AContent-Length%3A%20131%0D%0AHost%3A%20127.0.0.1%3A12233%0D%0AAccept%3A%20application%2Fjson%0D%0AUser-Agent%3A%20Python-urllib%2F2.7%0D%0AConnection%3A%20close%0D%0AContent-Type%3A%20application%2Fjson%3Bcharset%3DUTF-8%0D%0A%0D%0A%0D%0A">cache administrator</a> if you have difficulties authenticating yourself.</p>  <br> </div>  <hr>  <div id="footer"> <p>Generado Tue, 05 Nov 2013 19:44:22 GMT por squid.proxy (squid/3.1.19)</p> <!-- ERR_CACHE_ACCESS_DENIED --> </div> </body></html> ' 

我正在使用 selenium 2.34 和 Firefox 17。感谢任何帮助。

4

1 回答 1

3

经过4天找到解决此问题的方法后,我终于找到了。问题在于浏览器排除项,我指的是任何浏览器中代理配置中的选项卡。您需要在排除项中的任何浏览器中输入两个地址:

本地主机和 127.0.0.1

这非常重要,因为如果留下这会导致 Selenium 出现问题,因为它会尝试连接到上面提到的两个地址。

于 2013-11-07T13:19:42.653 回答