Is there any way to use the onresourcerequested callback of phantomjs in the Java driver?

I want to avoid loading CSS files in phantom JS I'm using a Java driver

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

My code uses phantom jsdriver (Scala) like this:

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

Is there a way to achieve the same functionality through the Java API?

Solution

I can use the phantom JS driver #executephantomjs method to register the onresourcerequested callback of the page

The following is an example of disabling Google Analytics requests:

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();" +
        "   }" +
        "};");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>