Extracting logs from a Selenium session

Vaibhav Tripathi
4 min readJan 2, 2019

Disclaimer: This blog is written with Chrome at its centre. If you work with some other browser, some parts may not be helpful to you. Though the general idea of collecting logs remains the same across browsers.

My Selenium test broke. This was a one-off. I did not change anything to my website or the test script. What might have gone wrong? Did the browser session die an unnatural death or was it the webdriver silently running into something unexpected?

If you work with selenium scripts, chances are you have had times where you wanted to throw the above questions at someone.

For a good amount of time now, I have been working on a Synthetic Monitoring Product, which means that I see selenium scripts and issues associated with them every day. This becomes increasingly challenging as I am working with scripts that I have not written, neither seen the websites that those scripts are testing. And yet, when things go south, I have to tell the customer exactly what went wrong and why did the test stall/broke. Not to mention, I need this information to keep my product highly robust.

What I thought would be a brilliant idea was that if I could collect logs when the tests ran, I can look for causes in those logs after my test has been completed. Of course, I can always delete the logs thus collected after my analysis is done, if you care about my disk space filling up rapidly. The logs that I am talking about are—console logs from the browser, logs from the webdriver and from the selenium grid server.

Logs from selenium webdriver (grid) server:

This is achievable by a simple option when starting the webdriver server. If you are familiar with the selenium server launch command — java -jar selenium-standalone.jar , you can get all logs from the webdriver server by adding a really helpful command-line argument to turn the above into —

java 
-Dselenium.LOGGER.level=INFO
-log Path/to/webdriver-server.log
-jar selenium-standalone.jar

Just to state the obvious, all logs will be dumped to “webdriver-server.log” file.

Logs from webdriver:

Since there can be a lot of idiosyncrasies here in terms of the browser we are running the tests on, the driver versions and what not, and I don’t want to delve into a lot of details for each variant, I will discuss things for chrome hereafter. This means that I will talk about how can we best extract logs from chromedriver.

We have a couple of options here.

1. Selenium’s get_log() API:

Selenium offers an API which we can use to demand logs by making a remote call to the selenium web server, for example in Python as follows:

driver_logs = driver.get_log(“driver”)

2. The ‘webdriver.chrome.logfile’ environment property:

We can also set a command-line option while launching the web driver server that will instruct the webdriver process to dump logs to a particular location on the disk. The above webdriver server launch command can be augmented like this:

java 
-Dselenium.LOGGER.level=INFO
-log Path/to/webdriver-server.log
-Dwebdriver.chrome.logfile=/Path/to/chromedriver.log
-jar selenium-standalone.jar

To compare the above two approaches, both can be pretty useful depending on how do we want to use the captured logs. Approach (1) is better when you want to log to a new file on every test because it gives you full control over how you write the captured logs. You can also filter the logs before writing them. The one drawback that I feel with this is that it will incur an additional network call on the webdriver server which can be time consuming if the logs are huge and the server is remote.

Logs from the browser:

There are options here too. First one is the same get_log() API that I mentioned to get the driver logs above.

1. Selenium’s get_log() API:

In case of browser logs, you just need to specify the type of the log as “browser”.

driver_logs = driver.get_log(“browser”)

2. Customise the user data directory:

On every OS, there is a default directory that Chrome stores its data in. You can find that directory by launching chrome and visiting chrome://version Remove “Default” from the rear of the “Profile Path” and it gives you the data directory. When you launch the browser with enable-logging and log-level=0 options, you will find a chrome_debug.log file being generated in the user data directory. The best part is, you can also overwrite the default data directory using a path of your own with the help of user-data-dir option while launching chrome. Please be noted that this will bring not just the logs but many other files to the custom directory that you define.

The best part that I like about alternative (2) here is that if you are launching your browser with a chrome extension, it will capture logs from within the extension by default, which you won’t get in (1).

Using the above suggestions, you should be able to capture all relevant logs during a selenium test. If the test breaks, at least one of these should be able to give you some insight about the problem that occurred.

Hope this was worth the time.

Feel free to comment and start a conversation. Thanks!

--

--