5

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.

StackTrace
  • 221
  • 5
  • 12

1 Answers1

3

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

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

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;
}
Toolkit
  • 10,779
  • 8
  • 59
  • 68
  • Thank you.. all samples on this topic deal with Java, and ChromePerformanceLoggingPreferences seems to be .Net specific. – Adarsha May 15 '17 at 01:24
  • I am also doing it with `webdriverio`, desiredCapabilities: { browserName: 'chrome', loggingPrefs: { 'browser': 'ALL', 'driver': 'ALL', 'performance': 'ALL' }, chromeOptions: { perfLoggingPrefs: { traceCategories: 'performance' } } } and then var logs = await client.log('performance'); – Toolkit May 15 '17 at 07:12
  • it seems this code is not working anymore `unknown error: cannot parse capability: goog:chromeOptions from unknown error: cannot parse perfLoggingPrefs from unknown error: unrecognized performance logging option: enableTimeline` funny is it is complaining about enableTimeline, which is not used at all – Jan Feb 04 '18 at 22:50
  • @Jan I have switched to Node.js and only can help by giving you my code if you need – Toolkit Feb 05 '18 at 06:23
  • @Toolkit, did you write the Node app in VS? Certainly I would like to see it. – Jan Feb 07 '18 at 11:52