Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Phantomjs无法打开浏览器打开的页面_Javascript_Jquery_Phantomjs - Fatal编程技术网

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

Javascript Phantomjs无法打开浏览器打开的页面,javascript,jquery,phantomjs,Javascript,Jquery,Phantomjs,我有一个Phantomjs脚本,它试图打开一个url。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 但是当我打开urlhttp:

我有一个Phantomjs脚本,它试图打开一个url。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
但是当我打开url
http://foo.bar/tree/nav_value/27
使用chrome浏览器,没有问题,页面加载正确

以下是脚本:

// 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);
    }
});
编辑:

奇怪的事情发生了。当我在另一台机器上的windows上使用phantomjs运行此代码时,它可以正常工作。但是在Ubuntu上它返回错误

phantomjs试图打开的url位于同一服务器上。(Ubuntu)


问题出在哪里?

不确定这是否会有帮助,但我有一些想法帮助我解决了过去使用PhantomJS时遇到的问题

首先,正如您所说,它可以在另一台机器上工作,您可能希望通过下载可执行文件并在Python脚本上指定路径来进行测试。1.9.8版在过去帮助我绕过了一些安全限制(我还保留了一些设置,以防它感兴趣)

您还可以尝试看看升级Selenium是否有帮助

pip install selenium --upgrade
另一个可能有助于理解发生了什么的想法是,在错误发生之前,尝试打印屏幕截图并记录页面源代码。你可以这样做:

# 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)

请让我知道这是否有帮助

但是当我用chrome打开url时
-你会用chrome发出GET请求吗?注意,您使用PhantomJS发出POST请求。是的,我发出GET请求。这有什么不同吗?取决于它能带来什么。为了实验起见,现在尝试在PhantomJS中请求与GET请求相同的页面。如果仍然找不到该页面。。。剧本中会有打字错误吗?遗憾的是,你不能显示网站的URL,我有一些其他的理论来解释为什么会出现这个错误。我把它和GET一起使用。仍然不起作用。它可以在另一台机器上的windows上工作,但不能在linux上工作。真奇怪@Navid777,请尝试通过
curl
wget
从与站点相同的Ubuntu服务器打开
foo.bar/tree/nav_value/27
。有错误吗?
# 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)