在ubuntu系统上,我试图在python-selenium测试中使用'browsernmob-proxy'。在文档here之后我安装了'browsermob-proxy'并尝试了以下python代码:
from selenium import webdriver
from browsermobproxy import Server
server = Server("/home/adietz/Projects/Venv/nosetests/lib/python2.7/site-packages/browsermobproxy/server.py")
server.start()
但是这立即失败并出现以下错误:
======================================================================
ERROR: test_example2.TestSuite.test_network
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/adietz/Projects/Venv/nosetests/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/home/adietz/Projects/Jenkins/bsp-usecase-tests/selenium/test_example2.py", line 41, in test_network
server.start()
File "/home/adietz/Projects/Venv/nosetests/local/lib/python2.7/site-packages/browsermobproxy/server.py", line 113, in start
stderr=subprocess.STDOUT)
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied
知道如何解决这个问题吗?
我也尝试使用
server = Server("/home/adietz/Projects/Venv/nosetests/lib/python2.7/site-packages/browsermobproxy")
相反,但由于以下错误而失败:
======================================================================
ERROR: test_example2.TestSuite.test_network
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/adietz/Projects/Venv/nosetests/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/home/adietz/Projects/Jenkins/bsp-usecase-tests/selenium/test_example2.py", line 40, in test_network
server = Server("/home/adietz/Projects/Venv/nosetests/lib/python2.7/site-packages/browsermobproxy")
File "/home/adietz/Projects/Venv/nosetests/local/lib/python2.7/site-packages/browsermobproxy/server.py", line 81, in __init__
"in path provided: %s" % path)
ProxyServerError: Browsermob-Proxy binary couldn't be found in path provided: /home/adietz/Projects/Venv/nosetests/lib/python2.7/site-packages/browsermobproxy
附录
browsermob-proxy
到端口8088或8089(不完全确定),但示例代码仍然无法运行.... 答案 0 :(得分:-1)
我猜,你为代理服务器提供了错误的路径。您的代码指向browsermob代理python绑定而不是服务器。请按照以下步骤操作。
从https://github.com/lightbody/browsermob-proxy/releases位置下载browsermob代理,并将该文件解压缩到特定目录。我已解压缩/解压缩到目录C:\\Projects\\BrowserMobProxy
如果尚未安装,请安装browsermob代理绑定。 pip install browsermob-proxy
然后您可以在编码中指向服务器路径,如下所示。我正在使用Windows。如下所示。
from browsermobproxy import Server
server = Server("C:\\Projects\\BrowserMobProxy\\bin\\browsermob-proxy")
server.start()
proxy = server.create_proxy()
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
proxy.new_har("google")
driver.get("http://www.google.co.uk")
proxy.har # returns a HAR JSON blob
server.stop()
driver.quit()
请相应地更改代理路径(您已下载或解压缩的位置)。