记录 Selenium 命令

获取Selenium的执行信息.

启用日志记录是获取额外信息的宝贵方法, 这些信息可能有助于您确定 遇到问题的原因.

获取一个logger

Java 日志通常是按类创建的. 您可以通过默认logger来使用所有loggers. 为了过滤特定类, 请参考 过滤器

获取根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日志并不简单直接, 如果您只是在寻找一种简单的方法 查看重要的Selenium日志, 请参阅 Selenium Logger 项目

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>