【问题标题】:How to capture network traffic with selenium如何使用 selenium 捕获网络流量
【发布时间】:2019-11-04 10:27:03
【问题描述】:

我正在开始一个新的 Django 项目,我正在尝试使用 Selenium 捕获网络流量。

我已经用 Selenium-wire(MITM 代理)实现了这个目标,但是 Django 不喜欢使用 selenium-wire(必须用“--nothreading --noreload”启动服务器,连接错误...... )。 我正在寻找通过现代解决方案来实现这一目标,例如直接解析 firefox 的网络开发工具或使用 firefox 插件。 我正在使用 Firefox Geckodriver 进行测试。

    for x in range(0, 10):
                profile = profile_firefox()
                options = options_firefox()
                driver = webdriver.Firefox(firefox_profile=profile, options=options, executable_path='/Users/*****/Desktop/selenium-master/headless_browser/geckodriver')
                try:
                        driver.set_window_position(0, 0)
                        driver.set_window_size(randint(1024, 2060), randint(1024, 4100))
                        time.sleep(randint(3,10))
                        driver.get(url)
                        wait = ui.WebDriverWait(driver, 10)
                        time.sleep(randint(8,10))
                        if driver.find_element_by_xpath("//*[@id=\"container\"]/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button"):
                                driver.find_element_by_xpath("//*[@id=\"container\"]/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button").click()
                                del driver.requests
                                time.sleep(randint(8,10))
                                driver.find_element_by_xpath("//*[@id=\"container\"]/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button").click()
                                time.sleep(randint(10,20))
                                for request in driver.requests:
                                        if request.path == "https://api.*********.**/*******/*******":
                                                request_api = request
                                                raw = str(request_api.body)
                                                request_api = raw.split(('b\''))
                                                payload_raw = request_api[1]
                                                payload = payload_raw[:-1]
                                                if payload:
                                                        header = request.headers
                                                        time.sleep(8)
                                                        break

                except:
                        print("Houston on a eu un probleme")
                        firefox_closing(driver)

编辑:


def profile_firefox():
        profile = FirefoxProfile()
        profile.set_preference('permissions.default.image', 2)
        profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')
        profile.set_preference("general.useragent.override", firefox_init())
        profile.set_preference('network.proxy.type', 1)
        profile.set_preference('network.proxy.socks', 'localhost')
        profile.set_preference('network.proxy.socks_port', 9050)
        profile.set_preference("network.proxy.socks_remote_dns", False)
        profile.set_preference("driver.privatebrowsing.autostart", True)
        profile.update_preferences()
        return profile


使用 Socks、HTTP、SSL 配置测试 2:


server = Server('/Users/*****/Desktop/selenium-master/browsermob-proxy-2.1.4/bin/browsermob-proxy')
server.start()
proxy = server.create_proxy()
proxy.selenium_proxy()#Dont understand what it does ???
port = int(proxy.port)

profile = FirefoxProfile()
profile.set_preference('permissions.default.image', 2)
profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')
profile.set_preference('general.useragent.override', firefox_init())
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', 'localhost')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference('network.proxy.ssl', 'localhost')
profile.set_preference('network.proxy.ssl_port', port)
profile.set_preference('network.proxy.http', 'localhost')
profile.set_preference('network.proxy.http_port', port)
profile.set_preference('network.proxy.socks_remote_dns', False)
profile.set_preference('driver.privatebrowsing.autostart', True)
profile.update_preferences()


似乎 Http 代理覆盖了 socks 配置...

如果您对我的代码或解决方案有任何线索或建议,非常感谢。

【问题讨论】:

    标签: python django python-3.x selenium-webdriver


    【解决方案1】:

    您可以使用代理来捕获网络流量。 browsermob-proxy 与 Python 中的 selenium 配合得很好。您需要先下载 browsermob 可执行文件。这是 Firefox 的一段代码:

    from browsermobproxy import Server
    from selenium import webdriver
    
    server = Server('path_to_executable')
    server.start()
    proxy = server.create_proxy()
    profile  = webdriver.FirefoxProfile()
    profile.set_proxy(proxy.selenium_proxy())
    driver = webdriver.Firefox(firefox_profile=profile)
    proxy.new_har("file_name", options={'captureHeaders': True, 'captureContent': True})
    driver.get("your_url")
    proxy.wait_for_traffic_to_stop(1, 60)
    for ent in proxy.har['log']['entries']:
        print(ent)
    
    server.stop()
    driver.quit()
    

    【讨论】:

    • 感谢您的回答。我已经编辑了我的第一篇文章,我正在使用 Tor 在我的 Firefox 配置文件中通过 9050 端口轮换我的 IP。是否可以将您的解决方案与我的配置相结合?
    • 我不知道它是否兼容我从未使用过 Tor。也许代理之间会有一些端口/主机重定向。
    • 我被卡住了,当我在配置文件配置中手动设置 socks、http、ssl 代理时,socks 代理不起作用......我正在寻找这样的解决方案,如果配置文件配置不起作用link
    【解决方案2】:

    Browsermob 是正确的方法。

    我必须了解 browsermob 和 tor 的工作原理。 对于 Tor,您必须像这样启用 HTTPTunnelPort 配置。

    tor --HTTPTunnelPort 8088
    

    并配置 browsermob 以使用它。

    proxy_params = {'httpProxy': 'localhost:8088', 'httpsProxy': 'localhost:8088'}
    proxy_b = server.create_proxy(params=proxy_params)
    

    谢谢。

    【讨论】:

    • BrowserMob 代理自 2016 年以来不再维护。我建议在 github.com/browserup/browserup-proxy 使用 BrowserUp 代理分支。它是一个插入式替代品,可与 python browsermob 客户端一起使用。
    【解决方案3】:

    Python 有一个名为 selenium-wire 的包。您可以使用该软件包来捕获网络流量并验证它们。 selenium-wire 是 selenium 的扩展版本,它将具有 selenium 的所有功能以及用于捕获网络和验证的额外 API。以下是一篇文章的链接 https://sensoumya94.medium.com/validate-network-requests-using-selenium-and-python-3da5be112f7b

    以下是包的仓库

    https://github.com/wkeeling/selenium-wire

    示例代码 -

    from seleniumwire import webdriver  # Import from seleniumwire
    
    # Create a new instance of the Firefox driver
    driver = webdriver.Firefox()
    
    # Go to the Google home page
    driver.get('https://www.google.com')
    
    # Access requests via the `requests` attribute
    for request in driver.requests:
        if request.response:
            print(
                request.url,
                request.response.status_code,
                request.response.headers['Content-Type']
            )
    

    【讨论】:

      猜你喜欢
      • 2021-02-28
      • 2023-03-17
      • 2022-10-16
      • 2015-03-25
      • 1970-01-01
      • 2018-12-18
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多