Selenium Java:DriverService默认不再向控制台输出日志的解决方法
解决Selenium驱动日志提示问题
首先明确:你看到的这条INFO信息不是程序错误,它只是Selenium的告知性提示——默认情况下,驱动的日志不再发送到控制台了,你的程序本身可以正常执行。
如果要消除这个提示,或者重新将驱动日志输出到控制台,可参考以下两种方案:
1. 屏蔽INFO级别的日志
通过Java日志框架(如SLF4J+Logback/Log4j2)调整日志级别,把org.openqa.selenium.remote.service.DriverService的日志级别设为WARN及以上,就能过滤掉这类INFO提示。
以Logback为例,在项目的logback.xml配置文件中添加:
<configuration> <logger name="org.openqa.selenium.remote.service.DriverService" level="WARN"/> </configuration>
2. 配置驱动日志输出到控制台
如果需要将驱动日志重新输出到控制台,可以通过ChromeDriverService和ChromeOptions手动配置:
package Testcases; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.chrome.ChromeDriverService; import java.io.File; import java.io.IOException; public class First { public static void main(String[] args) throws IOException { // 指定ChromeDriver路径 File chromeDriverPath = new File("C:\\Users\\ashwi\\Downloads\\chromedriver_win32\\chromedriver.exe"); // 创建DriverService并配置日志输出到控制台 ChromeDriverService service = new ChromeDriverService.Builder() .usingDriverExecutable(chromeDriverPath) .withLogOutput(System.out) .build(); ChromeOptions options = new ChromeOptions(); WebDriver driver = new ChromeDriver(service, options); driver.get("https://www.example.com"); // 替换为实际要访问的URL driver.quit(); } }
另外注意:原代码中driver.get("url");需替换为有效的网址,否则程序会访问无效地址。
内容的提问来源于stack exchange,提问作者yogesh uh
相关产品推荐
相关产品推荐

