【问题标题】:Enable "Preserve log" in chrome programmatically using chromedriver使用 chromedriver 以编程方式在 chrome 中启用“保留日志”
【发布时间】:2017-04-05 04:53:07
【问题描述】:

如何为 chrome 开发人员设置启用保留日志选项->首选项->在导航时保留日志,使用 chromeoptions.add_argument 或通过将首选项添加到 DesiredCapabilities 或以任何其他方式以编程方式。

【问题讨论】:

    标签: selenium-webdriver google-chrome-devtools selenium-chromedriver


    【解决方案1】:

    您可以从 performance 日志中获取重定向。根据docsgithub answer,这是我在 C# 中所做的,应该可以移植到 Python 中:

    var options = new ChromeOptions();
    var cap = DesiredCapabilities.Chrome();
    var perfLogPrefs = new ChromePerformanceLoggingPreferences();
    perfLogPrefs.AddTracingCategories(new string[] { "devtools.network" });
    options.PerformanceLoggingPreferences = perfLogPrefs;
    options.AddAdditionalCapability(CapabilityType.EnableProfiling, true, true);
    ptions.SetLoggingPreference("performance", LogLevel.All);
    var driver = new ChromeDriver(options);
    var url = "https://some-website-that-will-redirect.com/";
    driver.Navigate().GoToUrl(url);
    var logs = driver.Manage().Logs.GetLog("performance"); //all your logs with redirects will be here
    

    循环通过logs,如果message.params.redirectResponse.url等于原始URL,那么message.params.request.url将包含重定向URL

    Node.JS 使用webdriverio:

    var options = {
        desiredCapabilities: {
            browserName: 'chrome',
            loggingPrefs: {
                'browser': 'ALL',
                'driver': 'ALL',
                'performance': 'ALL'
            },
            chromeOptions: {
                perfLoggingPrefs: {
                    traceCategories: 'performance'
                },
            }
        }
    var client = webdriverio.remote(options);
    await client.url(url);
    var logs = await client.log('performance');
    var navigations = parseLogs(logs, url);
    
    function parseLogs(logs, url) {
        var redirectList = [];
        while (true) {
            var targetLog = (logs.value.find(l => {
                if (l.message.indexOf(url) == -1)
                    return false;
                var rootMessage = JSON.parse(l.message);
                if (((((rootMessage || {}).message || {}).params || {}).redirectResponse || {}).url == url)
                    return true;
                return false;
            }) || {}).message;
            if (!targetLog)
                break;
            if (redirectList.indexOf(url) != -1)
                break;
            redirectList.push(url);
            var targetLogObj = JSON.parse(targetLog);
            var nextUrl = ((((targetLogObj || {}).message || {}).params || {}).request || {}).url;
    
            if (nextUrl) {
                url = nextUrl;
                continue;
            }
            break;
        }
        return redirectList;
    }
    

    【讨论】:

    • 谢谢.. 这个主题的所有示例都处理 Java,ChromePerformanceLoggingPreferences 似乎是特定于 .Net 的。
    • 我也在使用webdriverio,desiredCapabilities: { browserName: 'chrome', loggingPrefs: { 'browser': 'ALL', 'driver': 'ALL', 'performance': 'ALL' }, chromeOptions: { perfLoggingPrefs: { traceCategories: 'performance' } } } 然后 var logs = await client.log('performance');
    • 似乎这段代码不再工作了unknown error: cannot parse capability: goog:chromeOptions from unknown error: cannot parse perfLoggingPrefs from unknown error: unrecognized performance logging option: enableTimeline 有趣的是它在抱怨 enableTimeline,它根本没有使用
    • @Jan 我已经切换到 Node.js,只有在需要时才能提供我的代码来提供帮助
    • @Toolkit,你是用 VS 写的 Node 应用吗?我当然想看看。
    猜你喜欢
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 2017-07-23
    相关资源
    最近更新 更多