首页 > 代码库 > 如何用selenium webdriver 捕获js error

如何用selenium webdriver 捕获js error

### 问题

捕捉页面上js error

 

### 解决办法

从Selenium webdriver log 中解析

# -*- coding:utf8 -*-import unittestfrom selenium import webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesclass CaptureJSError(unittest.TestCase):    @classmethod    def setUp(self):        self.driver = webdriver.Remote(http://yourseleniumgrid:4444/wd/hub,                                       desired_capabilities=DesiredCapabilities.FIREFOX)    def test_search_in_python_org(self):        driver = self.driver        driver.get("http://foopagewitherr:80")        logs = driver.get_log(browser)        for log in logs:            if log[level] == SEVERE:                print log                # output is {u‘timestamp‘: 1474618002360, u‘message‘: u‘TypeError: $(...).ready_ is not a function‘, u‘level‘: u‘SEVERE‘}    @classmethod    def tearDown(self):        self.driver.close()if __name__ == "__main__":    unittest.main()

 

Note:

这里的webdriver是由Selenium Grid提供的,本地webdriver 应该也是可以的

 

### 引用

https://www.3pillarglobal.com/insights/how-to-capture-javascript-errors-from-your-web-application

 

如何用selenium webdriver 捕获js error