启用“保留日志"在chrome中使用chromedriver编程 [英] Enable "Preserve log" in chrome programmatically using chromedriver

查看:243
本文介绍了启用“保留日志"在chrome中使用chromedriver编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to enable preserve log option for chrome developer settings->Preferences->Preserve log upon navigation, using chromeoptions.add_argument or by adding the pref to DesiredCapabilities or any other way programmatically.

推荐答案

您可以从performance日志中获取重定向.根据文档

You can get redirects from performance logs. According to docs and github answer here is what I've done in C#, should be possible to port in 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

Looping through logs, if message.params.redirectResponse.url is equal to original URL then message.params.request.url will contain redirect URL

使用webdriverio的Node.JS:

Node.JS using 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;
}

这篇关于启用“保留日志"在chrome中使用chromedriver编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆