火狐浏览器的Webdriver日志

14

无论是Chrome还是PhantomJS的selenium驱动程序都可以记录浏览器端发生的所有事情。在初始化驱动程序时,通过指定服务日志路径,您可以控制日志的写入位置。例如,对于Python中的Chrome:

from selenium import webdriver

driver = webdriver.Chrome(service_log_path="/tmp/log")
driver.get("http://www.google.com")
driver.close()

执行代码后,/tmp/log文件将包含服务日志,这对于调试非常有帮助。
[0.985][INFO]: Launching chrome: ...
[2.620][INFO]: RESPONSE InitSession {
   "acceptSslCerts": true,
   "applicationCacheEnabled": false,
   "browserConnectionEnabled": false,
   "browserName": "chrome",
   "chrome": {
      "userDataDir": "/var/folders/yy/ppdg927x4zv8b0rbzg1f_jzh0000gn/T/.org.chromium.Chromium.ibsof9"
   },
   "cssSelectorsEnabled": true,
   "databaseEnabled": false,
   "handlesAlerts": true,
   "javascriptEnabled": true,
   "locationContextEnabled": true,
   "nativeEvents": true,
   "platform": "Mac OS X",
   "rotatable": false,
   "takesHeapSnapshot": true,
   "takesScreenshot": true,
   "version": "37.0.2062.120",
   "webStorageEnabled": true
}
[2.677][INFO]: Waiting for pending navigations...
[2.696][INFO]: Done waiting for pending navigations
[3.290][INFO]: Waiting for pending navigations...
[4.338][INFO]: Done waiting for pending navigations
[4.338][INFO]: RESPONSE Navigate
[4.339][INFO]: COMMAND CloseWindow {

}
[4.451][INFO]: RESPONSE CloseWindow

有没有一种方法可以使用Firefox Web驱动程序获取相同的信息?
从源代码中看到,ChromePhantomJS都通过subprocess启动新服务并将--log-path参数传递给它。这些服务负责日志记录。至于Firefox驱动程序,它的实现方式非常不同,并且基于FirefoxBinary类。
提供的示例和链接与Python相关,但问题基本上是通用的,与语言无关。希望能得到任何指导。
2个回答

12

您需要在Firefox配置文件中设置日志记录选项,就像开发人员技巧链接中所述 - https://code.google.com/p/selenium/wiki/DeveloperTips。要使用控制台日志,请使用以下内容:

FirefoxProfile p = new FirefoxProfile();
p.setPreference("webdriver.log.file", "/tmp/firefox_console");
WebDriver driver = new FirefoxDriver(p);

对于浏览器日志,您应该使用

webdriver.firefox.logfile 

(https://code.google.com/p/selenium/wiki/FirefoxDriver)

希望这可以帮助您。


日志文件不像Chrome和PhantomJS服务提供的那么详细,但这是一个好消息,会有助于调试。非常感谢! - alecxe
你们知道 Chrome 浏览器日志的代码是什么吗?例如 webdriver.firefox.logfile 但是针对 Chrome。 - Leo Gallucci

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接