Selenium - 等待网页请求外部 URL

Selenium - Wait for the webpage to request external URL

我基本上是在尝试将 link 转换为 m3u8 文件。当页面首次加载时,它会请求带有每天更改的秘密令牌的 m3u8 文件。如果页面完全加载,则会下载该文件。我可以在网络选项卡上使用 chrome 开发人员工具查看它。由于这个令牌是动态生成的,我需要让网站先请求这个文件,然后给我 URL 到包含令牌的文件(类似于 http://secret-website.com/file?token=342782g1bud1)。

我从未使用过 selenium,所以我想知道如果可能我该怎么做。我需要使用 python 或 c# 以编程方式执行此操作。

我找到了一个解决方案,它会等到找到 .m3u8 文件。我使用 fiddlecore 作为代理,所以我可以看到浏览器正在发送什么。这将让我捕获任何请求,如果请求包含 .m3u8,它将简单地打印出 URL(我需要)。请注意,这不适用于 https,因为 fiddlecore 需要认证(我想这很容易修复)。这是一个简单的工作代码。

   bool close = false;

    // - Initialize fiddler for http proxy

    Fiddler.FiddlerApplication.Startup(5000, true, false);
    Fiddler.FiddlerApplication.BeforeResponse += delegate(Session s)
    {
        string path = s.fullUrl;
        if (path.Contains("720p.m3u8"))
        {
            Console.WriteLine(path);
            close = true;
        }
    };

    // - Create a selenium proxy
    var seleniumProxy = new OpenQA.Selenium.Proxy { HttpProxy = "localhost:5000"};
    var profile = new FirefoxProfile();
    profile.SetProxyPreferences(seleniumProxy);

    // - Initialize selenium for browser automation
    IWebDriver driver = new FirefoxDriver(profile);
    driver.Navigate().GoToUrl("http://www.asite");
    while (!close) { }

    driver.Quit();
    Fiddler.FiddlerApplication.Shutdown();