0

在 ubuntu 系统上,我正在尝试将“browsernmob-proxy”与 python-selenium 测试一起使用。按照此处的文档,我安装了“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 上获得了 tun(不完全确定),但示例代码仍然无法运行......
4

1 回答 1

-1

我猜,您为代理服务器提供了错误的路径。您的代码指向 browsermob 代理 python 绑定而不是服务器。请按照以下步骤操作。

  1. 从位置https://github.com/lightbody/browsermob-proxy/releases下载 browsermob 代理并将文件解压缩到特定目录。我已提取/解压缩到目录C:\\Projects\\BrowserMobProxy

  2. 如果尚未安装,请安装 browsermob 代理绑定。pip install browsermob-proxy

  3. 然后您可以在编码中指向服务器路径,如下所示。我正在使用窗户。如下所示。

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

请相应地更改代理路径(您下载或提取的位置)。

于 2018-01-11T08:57:10.400 回答