使用Selenium从Firefox获取console.log输出 [英] Getting console.log output from Firefox with Selenium
问题描述
我试图通过python Selenium API绑定获取网页的 console.log
Firefox输出。根据 Chrome代码 ,以及一些来自文档的建议,我尝试了以下方法: / p>
from selenium import webdriver $ b $ from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
d = DesiredCapabilities.FIREFOX
d ['loggingPrefs'] = {'browser':'ALL'}
fp = webdriver.FirefoxProfile()
fp.set_preference('webdriver.log.file','/ tmp / firefox_console ')
driver = webdriver.Firefox(capabilities = d,firefox_profile = fp)
driver.set_window_size(1280,1024)
driver.get('http://foo.com')
试试:
WebDriverWait(driver,10).until(lambda driver:driver.execute_script(return document.readyState)==complete)
('browser'):
打印条目
finally:
driver.quit()
但是,即使是一个调用 console.log(foo)
的简单示例页面,我也看不到foo
或者通过API返回的日志条目或者在 / tmp / firefox_console
文件中。难道我做错了什么?或者这是一个硒限制?
您的代码在 get_log
函数中是正确的,只需在最后添加 print
语句即可:
来自硒import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
$ b#enable浏览器记录
d = DesiredCapabilities.FIREFOX
d ['loggingPrefs'] = {'browser' :'ALL'}
driver = webdriver.Firefox(capabilities = d)
#加载一些网站
driver.get('http://foo.com')
#打印信息
用于输入driver.get_log('browser'):
打印输入
打印
driver.quit()
实际上:
print len(driver.get_log('browser'))
返回 53
在我的例子中,作为列表中的示例条目:
$ p $ {u'timestamp' :1407591650751,u'message': üExpected:'but found'}'。Declaration dropped。,u'level':u'WARNING'}
看起来像一个坏字符问题。至于为什么在 / tmp / firefox_console
文件中没有输出,我没有任何线索,记录器似乎抛出一些webdriver调试信息,但没有 console.log
输出
编辑:显然上面的代码不会从
的console.log
。这不是一个硒错误,据我所知,但与Firefox的问题。我设法通过安装 Firebug a>与 ConsoleExport 插件,然后将其指向某个日志记录服务器。另见 this SO答案有关如何从Selenium以编程方式启用Firebug的详细信息。 有关更多详细信息,请参阅此主题: https://gist.github.com/CGenie/fc63536a8467ae6ef945
I'm trying to get a web page's console.log
output from Firefox via the python Selenium API bindings. Based on the code for Chrome, and some advice from the documentation, I tried the following:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
d = DesiredCapabilities.FIREFOX
d['loggingPrefs'] = { 'browser':'ALL' }
fp = webdriver.FirefoxProfile()
fp.set_preference('webdriver.log.file', '/tmp/firefox_console')
driver = webdriver.Firefox(capabilities=d,firefox_profile=fp)
driver.set_window_size(1280,1024)
driver.get('http://foo.com')
try:
WebDriverWait(driver,10).until(lambda driver: driver.execute_script("return document.readyState") == "complete")
for entry in driver.get_log('browser'):
print entry
finally:
driver.quit()
But, for even a simple example page that calls console.log("foo")
, I don't see "foo"
either in the log entries returned via the API or in the /tmp/firefox_console
file. Am I doing something wrong? Or is this a Selenium limitation?
Your code is correct when it comes to the get_log
function, just add a print
statement at the end like so:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# enable browser logging
d = DesiredCapabilities.FIREFOX
d['loggingPrefs'] = {'browser': 'ALL'}
driver = webdriver.Firefox(capabilities=d)
# load some site
driver.get('http://foo.com')
# print messages
for entry in driver.get_log('browser'):
print entry
print
driver.quit()
In fact:
print len(driver.get_log('browser'))
returns 53
in my example with this as a sample entry in the list:
{u'timestamp': 1407591650751, u'message': u"Expected ':' but found '}'. Declaration dropped.", u'level': u'WARNING'}
Seems like a bad char problem. As for why there is no output in the /tmp/firefox_console
file, I have no clue, the logger seems to throw some webdriver debug info but no console.log
output.
EDIT: Apparently the above code does not return data from console.log
. It's not a Selenium bug as far as I can tell but a problem with Firefox. I managed to get around it by installing the Firebug along with ConsoleExport plugin for Firebug, then point it to some logging server. See also this SO answer for details on how to enable Firebug programmatically from Selenium.
See this gist for more details: https://gist.github.com/CGenie/fc63536a8467ae6ef945
这篇关于使用Selenium从Firefox获取console.log输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!