【发布时间】:2017-04-05 04:53:07
【问题描述】:
如何为 chrome 开发人员设置启用保留日志选项->首选项->在导航时保留日志,使用 chromeoptions.add_argument 或通过将首选项添加到 DesiredCapabilities 或以任何其他方式以编程方式。
【问题讨论】:
标签: selenium-webdriver google-chrome-devtools selenium-chromedriver
如何为 chrome 开发人员设置启用保留日志选项->首选项->在导航时保留日志,使用 chromeoptions.add_argument 或通过将首选项添加到 DesiredCapabilities 或以任何其他方式以编程方式。
【问题讨论】:
标签: selenium-webdriver google-chrome-devtools selenium-chromedriver
您可以从 performance 日志中获取重定向。根据docs 和github 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;
}
【讨论】:
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,它根本没有使用