gpt4 book ai didi

selenium-webdriver - 使用 chromedriver 以编程方式在 chrome 中启用 "Preserve log"

转载 作者:行者123 更新时间:2023-12-02 01:19:48 26 4
gpt4 key购买 nike

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

最佳答案

您可以从 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;
}

关于selenium-webdriver - 使用 chromedriver 以编程方式在 chrome 中启用 "Preserve log",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40726192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com