简体   繁体   English

Phantomjs无法打开浏览器打开的页面

[英]Phantomjs cannot open a page that browser opens

I have a Phantomjs script that tries to open a url. 我有一个Phantomjs脚本试图打开一个网址。 phantomjs returns this error: phantomjs返回此错误:

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! 但是当我用Chrome浏览器打开网址http://foo.bar/tree/nav_value/27时,没有问题,页面正确加载!

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. 当我在另一台机器上的Windows上使用phantomjs运行此代码时,它可以工作。 But on Ubuntu it returns the error! 但在Ubuntu上它返回错误!

The url that phantomjs is trying to open is on the same server. phantomjs尝试打开的URL位于同一台服务器上。 (Ubuntu) (Ubuntu的)

What is the problem? 问题是什么?

Not sure this will help, but I have some ideas that helped me figure out problems with PhantomJS in the past. 不确定这会有所帮助,但我有一些想法可以帮助我解决过去PhantomJS的问题。

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. 首先,正如您所说它适用于另一台机器,您可能希望通过下载可执行文件并在Python脚本上指定路径来测试其他版本的PhantomJS Version 1.9.8 helped me with bypassing some security restrictions in the past (I also left some settings in case it may interest). 版本1.9.8帮助我绕过了一些安全限制(我还留下了一些设置以防它可能感兴趣)。

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. 您也可以尝试查看升级Selenium是否有帮助。

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! 请让我知道这可不可以帮你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM