Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phantomjs cannot open a page that browser opens

I have a Phantomjs script that tries to open a url. phantomjs returns this error:

Unable to load resource (request ID:undefinedURL:http://foo.bar/tree/nav_value/27)
Error code: 203. Description: Error downloading http://foo.bar/tree/nav_value/27 - server replied: Not Found

But when I open the url http://foo.bar/tree/nav_value/27 with chrome browser, there's no problem and the page is loaded correctly!

This is the script:

// Read the Phantom webpage '#intro' element text using jQuery and "includeJs"

"use strict";
var page = require('webpage').create();
var system = require('system');

if (system.args.length != 2) {
    console.log("please pass 2 argument")
}
var company_id = system.args[1]
console.log("c", company_id)

page.onConsoleMessage = function(msg) {
    console.log("message", msg);
};

page.onResourceError = function(resourceError) {
    console.log('Unable to load resource (request ID:' + resourceError.id + 'URL:' + resourceError.url + ')');
    console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);

};

page.onError = function(msg, trace) {
    console.log("error", msg)
}

var nav_value;

page.open("http://foo.bar/tree/nav_value/27", 'post', 'username=navid&password=test', function(status) {
    if (status === "success") {
        page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
            page.evaluate(function() {
                nav_value = parseInt($("#value").text());
            });
            phantom.exit(0);
        });
    } else {
      phantom.exit(1);
    }
});

EDIT:

Something odd happens. When I run this code with phantomjs on windows on another machine it works. But on Ubuntu it returns the error!

The url that phantomjs is trying to open is on the same server. (Ubuntu)

What is the problem?

like image 501
Navid777 Avatar asked Nov 09 '22 09:11

Navid777


1 Answers

Not sure this will help, but I have some ideas that helped me figure out problems with PhantomJS in the past.

First, as you say it works on another machine, you may want to test other versions of PhantomJS, by downloading the executable and specifying the path on your Python script. Version 1.9.8 helped me with bypassing some security restrictions in the past (I also left some settings in case it may interest).

driver = webdriver.PhantomJS(
    executable_path='/path/to/the/downloaded/phantomjs19',
    # you can specify args, such as:
    service_args=[
        '--ignore-ssl-errors=true', 
        '--ssl-protocol=any', 
        '--web-security=false',
    ],
    # and also other capabilities:
    desired_capabilities={
        'phantomjs.page.settings.resourceTimeout': '5000',
        'phantomjs.page.settings.userAgent': (
            "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 "
            "(KHTML, like Gecko) Chrome/15.0.87"
        ),
    },
)

You may also try to see if upgrading Selenium helps.

pip install selenium --upgrade

Another idea that might help to understand what is happening is to try to print a screenshot and log the page source before the error happens. You can do it like:

# Set the window size to something appropriate for your tests.
driver.set_window_size(900, 800)
driver.save_screenshot('screen.png')

# Check if the page source matches your expectations.
with open('temp.html', 'w') as f:
    f.write(driver.page_source)

Please, let me know if this helps!

like image 138
Ivan Chaer Avatar answered Nov 14 '22 22:11

Ivan Chaer