Phantomjs cannot open a page opened by a browser

I have a Phantomjs script that is trying 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 the Chrome browser, there is no problem, and the page loads 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 strange is happening. When I run this code with phantomjs on windows on another machine, it works. But on Ubuntu it returns an error!

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

What is the problem?

+5
source share
1 answer

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

First, as you say, it works on a different machine, you can test other versions of PhantomJS by downloading the executable and specifying the path to your Python script. Version 1.9.8 helped me bypass some security restrictions in the past (I also left some settings in case this might be of 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 can also try to find out if updating Selenium helps.

 pip install selenium --upgrade 

Another idea that can help you understand what is happening is to try to print a screenshot and log the page source before an error occurs. 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!

+1
source

All Articles