pythonselenium浏览器console,使用Selenium从Firefox获取console.log输出

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值