Capturing JavaScript error in Selenium

前端 未结 12 717
盖世英雄少女心
盖世英雄少女心 2020-12-02 08:15

Is there a way to capture errors occurring in the DOM in Selenium and probably flag the same as an error in the page?

To give a brief exam

12条回答
  •  一生所求
    2020-12-02 08:26

    Here's the python webdriver solution I use:

    def check_browser_errors(driver):
        """
        Checks browser for errors, returns a list of errors
        :param driver:
        :return:
        """
        try:
            browserlogs = driver.get_log('browser')
        except (ValueError, WebDriverException) as e:
            # Some browsers does not support getting logs
            LOGGER.debug("Could not get browser logs for driver %s due to exception: %s",
                         driver, e)
            return []
    
        errors = []
        for entry in browserlogs:
            if entry['level'] == 'SEVERE':
                errors.append(entry)
        return errors
    

提交回复
热议问题