有没有办法在Java驱动程序中使用PhantomJS的onResourceRequested回调?

Gre*_*reg 3 java phantomjs selenium-webdriver

我想避免在PhantomJS中加载css文件.我正在使用Java驱动程序.我为PhantomJS看到了一个很好的示例js代码:

page.onResourceRequested = function(requestData, request) {
    if ((/http:\/\/.+?\.css/gi).test(requestData['url']) || requestData['Content-Type'] == 'text/css') {
        request.abort();
    }
};
Run Code Online (Sandbox Code Playgroud)

我的代码使用像这样的PhantomJSDriver(scala):

val sb = new PhantomJSDriverService.Builder()
val svc = sb.usingPhantomJSExecutable(new java.io.File("./phantomjs")).usingCommandLineArguments(Array("--load-images=false","--disk-cache=true")).build()
Run Code Online (Sandbox Code Playgroud)

有没有办法可以通过Java API实现相同的功能?

Ale*_*lov 12

我能够onResourceRequested使用该PhantomJSDriver#executePhantomJS方法注册页面的回调.

以下是禁用Google Analytic请求的示例:

PhantomJSDriver driver = new PhantomJSDriver(service, capabilities);
driver.executePhantomJS("this.onResourceRequested = function(request, net) {" +
        "   console.log('REQUEST ' + request.url);" +
        "   if (request.url.indexOf('google-analytics') !== -1) {" +
        "       console.log('Abort ' + request.url);" +
        "       net.abort();" +
        "   }" +
        "};");
Run Code Online (Sandbox Code Playgroud)