Logging Selenium commands

Getting information about Selenium execution.

Turning on logging is a valuable way to get extra information that might help you determine why you might be having a problem.

Getting a logger

Java logs are typically created per class. You can work with the default logger to work with all loggers. To filter out specific classes, see Filtering

Get the root logger:

30
32
<details class="mt-3">
  <summary>Show full example</summary>
  <div class="pt-2">
    <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">package</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">dev.selenium.troubleshooting</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline">

import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; import java.util.logging.FileHandler; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.openqa.selenium.manager.SeleniumManager; import org.openqa.selenium.remote.RemoteWebDriver; public class LoggingTest { @AfterEach public void loggingOff() { Logger logger = Logger.getLogger(""); logger.setLevel(Level.INFO); Arrays.stream(logger.getHandlers()).forEach(handler -> { handler.setLevel(Level.INFO); }); } @Test public void logging() throws IOException { Logger logger = Logger.getLogger(""); logger.setLevel(Level.FINE); Arrays.stream(logger.getHandlers()).forEach(handler -> { handler.setLevel(Level.FINE); }); Handler handler = new FileHandler("selenium.xml"); logger.addHandler(handler); Logger.getLogger(RemoteWebDriver.class.getName()).setLevel(Level.FINEST); Logger.getLogger(SeleniumManager.class.getName()).setLevel(Level.SEVERE); Logger localLogger = Logger.getLogger(this.getClass().getName()); localLogger.warning("this is a warning"); localLogger.info("this is useful information"); localLogger.fine("this is detailed debug information"); byte[] bytes = Files.readAllBytes(Paths.get("selenium.xml")); String fileContent = new String(bytes); Assertions.assertTrue(fileContent.contains("this is a warning")); Assertions.assertTrue(fileContent.contains("this is useful information")); Assertions.assertTrue(fileContent.contains("this is detailed debug information")); } }

<div class="text-end pb-2 mt-2">
  <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L31" target="_blank">
    <i class="fas fa-external-link-alt pl-2"></i>
    <strong>View full example on GitHub</strong>
  </a>
</div>

Java Logging is not exactly straightforward, and if you are just looking for an easy way to look at the important Selenium logs, take a look at the Selenium Logger project

Python logs are typically created per module. You can match all submodules by referencing the top level module. So to work with all loggers in selenium module, you can do this:

4
6
<details class="mt-3">
  <summary>Show full example</summary>
  <div class="pt-2">
    <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-py" data-lang="py"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">logging</span>

def test_logging(log_path): logger = logging.getLogger('selenium') logger.setLevel(logging.DEBUG) handler = logging.FileHandler(log_path) logger.addHandler(handler) logging.getLogger('selenium.webdriver.remote').setLevel(logging.WARN) logging.getLogger('selenium.webdriver.common').setLevel(logging.DEBUG) logger.info("this is useful information") logger.warning("this is a warning") logger.debug("this is detailed debug information") with open(log_path, 'r') as fp: assert len(fp.readlines()) == 3

<div class="text-end pb-2 mt-2">
  <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/python/tests/troubleshooting/test_logging.py#L5" target="_blank">
    <i class="fas fa-external-link-alt pl-2"></i>
    <strong>View full example on GitHub</strong>
  </a>
</div>