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

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

问题描述

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

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 日志中获取重定向.根据 docsgithub answer 这是我在 C# 中所做的,应该可以移植到 Python 中:

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会包含重定向网址

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

Node.JS 使用 webdriverio:

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天全站免登陆