Sta*_*ace 5 google-chrome-devtools selenium-chromedriver selenium-webdriver
如何启用 chrome 开发人员设置 -> 首选项 -> 在导航时保留日志选项,使用 chromeoptions.add_argument 或通过以编程方式将首选项添加到 DesiredCapability 或任何其他方式。
您可以从performance日志中获取重定向。根据文档和github 的答案,这是我在 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
Run Code Online (Sandbox Code Playgroud)
循环遍历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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4887 次 |
| 最近记录: |