Capturing JavaScript error in Selenium

前端 未结 12 736
盖世英雄少女心
盖世英雄少女心 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:37

    I would like to iterate on the answer of jhanifen. Here is a javascript solution that does not depend on jQuery. It creates an invisible HTML list on the bottom of the page, which contians the errors.

    (function () {
        var ul = null;
        function createErrorList() {
            ul = document.createElement('ul');
            ul.setAttribute('id', 'js_error_list');
            ul.style.display = 'none';
            document.body.appendChild(ul);
        }
        window.onerror = function(msg){
            if (ul === null)
                createErrorList();
            var li = document.createElement("li");
            li.appendChild(document.createTextNode(msg));
            ul.appendChild(li);
        };
    })();
    

提交回复
热议问题