Prevent CSS/other resource download in PhantomJS/Selenium driven by Python
Question: I'm trying to speed up Selenium/PhantomJS webscraper in Python by preventing
download of CSS/other resources. All I need to download is img src and alt
tags. I've found this code:

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

via: [how can I control phantomjs to skip download some kind of
resource](http://stackoverflow.com/questions/9486377/how-can-i-control-
phantomjs-to-skip-download-some-kind-of-resource)

_How/where can I implement this code in Selenium driven by Python? Or, is
there another better way to stop CSS/other resources from downloading?_

Note: I've already found how to prevent image download by editing service_args
variable via:

[How do I set a proxy for phantomjs/ghostdriver in python
webdriver?](http://stackoverflow.com/questions/14699718/how-do-i-set-a-proxy-
for-phantomjs-ghostdriver-in-python-webdriver?lq=1)

and

[PhantomJS 1.8 with Selenium on python. How to block
images?](http://stackoverflow.com/questions/15371495/phantomjs-1-8-with-
selenium-on-python-how-to-block-images?lq=1)

But service_args can't help me with resources like CSS. Thanks!


Answer: A bold young soul by the name of “watsonmw” [recently
added](https://github.com/detro/ghostdriver/commit/d9b65ed014ed9ff8a5e852cc40e59a0fd66d0cf1)
functionality to Ghostdriver (which Phantom.js uses to interface with
Selenium) that allows access to [Phantom.js API calls which require a page
object](http://phantomjs.org/api/webpage/), like the `onResourceRequested` one
you cited.

For a solution at all costs, consider building from source (which developers
note “takes roughly 30 minutes ... with 4 parallel compile jobs on a modern
machine”) and integrating his patch, linked above.

Then this (untested) Python code should work as a proof of concept:

    
    
    from selenium import webdriver
    driver = webdriver.PhantomJS('phantomjs')
    
    # hack while the python interface lags
    driver.command_executor._commands['executePhantomScript'] = ('POST', '/session/$sessionId/phantom/execute')
    
    driver.execute('executePhantomScript', {'script': '''
    page.onResourceRequested = function(requestData, request) {
        // ...
    }
    ''', 'args': []})
    

Until then, you’ll just get a `Can't find variable: page` exception.

Good luck! There are a lot of great alternatives, like working in a Javascript
environment, driving Gecko, proxies, etc.

