如何使用selenium捕获网络流量

新手上路,请多包涵

我正在开始一个新的 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 配置…

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

原文由 Neeko 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 626
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)

谢谢。

原文由 Neeko 发布,翻译遵循 CC BY-SA 4.0 许可协议

您可以使用代理来捕获网络流量。 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()

原文由 F Blanchet 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题