使用Selenium和PhantomJS将javascript polyfill注入页面

Mag*_*ova 5 javascript selenium webdriver polyfills es5-shim

在这里和其他地方已经有很多关于PhantomJS缺乏Function.prototype.bind方法的讨论,并且许多有用的慈善家已经编写过填充/填充或指向其他资源.我正在通过Selenium Webdriver和Python绑定实现PhantomJS.我已经尝试了几种方法来使用这种polyfill,但无济于事.目前我在我的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)
Run Code Online (Sandbox Code Playgroud)

我每次访问新页面时都会执行此代码.事实上,这种方法可以确保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)
Run Code Online (Sandbox Code Playgroud)

等等

我希望尽管与其他有关此.bind()问题的问题密切相关,但我的问题对于那些通常希望为其开箱即用的Selenium PhantomJS实现添加功能的人来说非常有用.如果我可以修改由我的Ghostdriver-PhantomJS-Selenium堆栈实现的JavaScript库,而不是直接将es5垫片添加到我访问的每个页面,我会喜欢它,但我不知道我该怎么做或者如果.如果我只是在裸PhantomJS上构建这个测试器而不是通过另一个框架过滤它,那么开始觉得这样的事情会更简单.

我的规格:我在Ubuntu 14.04 LTS(GNU/Linux 3.17.1-elastic x86_64)上通过Python 2.7使用Selenium版本1.43和PhantomJS 1.98.

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

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> 
Run Code Online (Sandbox Code Playgroud)

Zon*_*Zon 0

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