javascript - Selenium 的 PhantomJS Webdriver 未在 ReactJS 中加载页面

标签 javascript python-2.7 selenium reactjs phantomjs

我正在尝试测试网站的一项新功能。这是迄今为止用 React 构建的唯一页面。当我尝试使用 PhantomJS 在 Selenium 中运行测试时,页面索引会加载,但完整页面加载永远不会触发。

页面正文是:

<body>
    <main id="content"></main>
    <script type="text/javascript">
        function loadBundleJS( jsSource){
            var bundleJSScriptTag=document.createElement('script')
            bundleJSScriptTag.setAttribute("type","text/javascript")
            bundleJSScriptTag.setAttribute("src", jsSource)
            if (typeof bundleJSScriptTag != 'undefined'){
                document.getElementsByTagName('head')[0].appendChild(bundleJSScriptTag);
            }
        }
        var paramsArray = window.location.search.substring(1).split("&");
        Object.keys(paramsArray).forEach(function(key){
            var param = paramsArray[key];
            if (param.indexOf("/")>-1){
                param = param.substring(0, param.indexOf("/"))
            }
        })
        loadBundleJS('js/bundle.0.0.2.js')
    </script>
</body>

当网站在浏览器中运行时,内容将附加到主标记中。然而,在 PhantomJS 中,此内容永远不会被附加,并且 PhantomJS 会加载空白页面。

最佳答案

问题不在你的代码中,而是在 PhantomJS 运行的 WebKit 浏览器中。 PhantomJS 运行旧版本的 WebKit 引擎,该引擎使用旧版本的 ECMAScript。
ReactJS 使用 ECMAScript 5 中的 Function.bind 方法。
解决方案非常简单,如果 Function.prototype.bind 不存在,则需要在代码中定义它。

** 确保代码在之前加载,包括react.js。

if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
    if (typeof this !== 'function') {
        throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
    }

    var aArgs   = Array.prototype.slice.call(arguments, 1),
        fToBind = this,
        fNOP    = function() {},
        fBound  = function() {
            return fToBind.apply(this instanceof fNOP
                    ? this
                    : oThis,
                aArgs.concat(Array.prototype.slice.call(arguments)));
        };

    if (this.prototype) {
        fNOP.prototype = this.prototype;
    }
    fBound.prototype = new fNOP();

    return fBound;
};

}

代码取自:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind#Polyfill

关于javascript - Selenium 的 PhantomJS Webdriver 未在 ReactJS 中加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31271237/

相关文章:

Python: "import JSON... json.loads(request.body)"2.7->3.4

python - 关于 python MRO 以及 super() 的行为

python - 如何使用 selenium ChromeDriver 滚动 Google map 上的侧边栏以加载更多结果?

java - 如何通过 Selenium 测试用例启用或禁用地理定位

javascript - 如何禁用 dijit.form.NumberSpinner 小部件上的鼠标滚轮事件?

php - 如何在 javascript 中使用 UTF-8 字符设置 Safari 浏览器标题?

php - 如何使用 JavaScript 将数据 POST 到远程服务器?

javascript - 使用 Require.js 设置和获取全局变量

python - Odoo v9 列未在相关字段上排序的问题

python - 通过 Selenium Python 在正常/ headless 模式下使用 ChromeDriver/Chrome 访问 Cloudflare 网站有什么区别