Injecting a javascript polyfill onto a page with Selenium &a

Injecting a javascript polyfill onto a page with Selenium & PhantomJS

本文关键字:with Selenium page onto javascript polyfill Injecting      更新时间:2023-09-26

这里和其他地方都有很多关于PhantomJS缺乏Function.prototype.bind方法的讨论,许多乐于助人的慈善家编写了垫片/polyfill或向其他人介绍这些资源。我正在通过带有Python绑定的Selenium Webdriver实现PhantomJS。我试过几种方法来利用这种聚乙烯填充物,但都没有用。目前,我在我的webdriver继承测试程序类中使用以下代码:

    bindShim = """var script = document.createElement('script');
    script.src = '/home/dunkin/scripts/es5-shim.js';***
    script.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(script);
    """
    self.execute_script(bindShim)

每次访问新页面时,我都会执行此代码。事实上,这种方法可以确保PhantomJS理解jQuery变量。然而,我仍然在我的PhantomJS驱动程序日志中看到以下内容:

[ERROR - 2015-02-10T17:43:44.068Z] Session [fd37e5c0-b14b-11e4-b9e3-5bbebfaf3f9d] - page.onError - msg: TypeError: 'undefined' is not a function (evaluating 'arguments.callee.bind(this,e)')
[ERROR - 2015-02-10T17:43:44.069Z] Session [fd37e5c0-b14b-11e4-b9e3-5bbebfaf3f9d] - page.onError - stack:
  (anonymous function) (https://jsta.sh/media/all.js?1459:16)
  t (https://jsta.sh/media/all.js?1459:16)
  (anonymous function) (https://jsta.sh/media/all.js?1459:17)
  (anonymous function) (https://jsta.sh/media/all.js?1459:8)
  (anonymous function) (https://jsta.sh/media/all.js?1459:8)
  (anonymous function) (https://jsta.sh/media/all.js?1459:8)
  I (https://jsta.sh/media/all.js?1459:2)

等等。

我希望,尽管我的问题与这个.bind()问题的其他问题密切相关,但对于那些通常希望为其开箱即用的Selenium PhantomJS实现添加功能的人来说,我的问题是有用的。如果我能修改由我的Ghostdriver PhantomJS Selenium堆栈实现的JavaScript库,而不是直接在我访问的每个页面上添加es5填充程序,我会很高兴,但我不确定我该怎么做,也不确定是否可以。我开始觉得,如果我只是在裸PhantomJS上构建这个测试程序,而不是通过另一个框架过滤它,这些事情会更简单。

我的规格:

  • 硒版本1.43
  • PhantomJS 1.98
  • Python 2.7
  • Ubuntu 14.04 LTS(GNU/Linux 3.17.1-弹性x86_64)

***当我在phantomjs控制台中使用es5填充程序时,它会产生以下有希望的结果:

phantomjs> console.log(Object.keys)
function keys() {
    [native code]
}
undefined
phantomjs> var shim = require("/home/dunkin/scripts/es5-shim.js")
undefined
phantomjs> console.log(Object.keys)
function keys(object) {
        if (isArguments(object)) {
            return originalKeys(ArrayPrototype.slice.call(object));
        } else {
            return originalKeys(object);
        }
    }
undefined
phantomjs> 

当前2.1版本的phatomjs发行版中似乎包含了polyfill。请尝试下载最新的。