在C#项目中部署CasperJS

osk*_*ows 6 c# phantomjs casperjs

Tl:dr - 如何从C#项目文件夹中引用CasperJS和PhantomJS?

当我手动将CasperJS和PhantomJS的二进制文件解压缩到C:驱动器时,我有从C#项目运行CasperJS脚本的工作代码.(请参阅此处获取标签为WORKING Code的简单指南和工作代码:)

由于没有安装需要我认为这将是很容易将这些移动到C#项目文件夹,而不是\tools\casperjs\tools\phantomjs.另外,我需要在代码中使用更新PATH变量p.StartInfo.EnvironmentVariables["PATH"] = EnvPath;

我尝试的所有路径组合都会出现以下错误 "Fatal: [Errno 2] No such file or directory; did you install phantomjs?"

所有文件都已包含在文件路径中.我错过了一些明显的东西吗

非工作代码: [filepaths\tools\casperjs,\ tools\phantomjs&C:\ Python34]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Casper
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string Cpath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            //;C:\phantomjs;C:\casperjs\batchbin
            FileInfo csp1 = new FileInfo(Cpath + @"\tools\casperjs\n1k0-casperjs-4f105a9\bin\casperjs");
            FileInfo csp2 = new FileInfo(Cpath + @"\tools\casperjs\batchbin");
            FileInfo pht = new FileInfo(Cpath + @"\tools\phantomjs");
            string EnvPath = string.Format(";{0};{1}", pht, csp2);

            DirectoryInfo dir = csp1.Directory;
            FileInfo path = new FileInfo(@"C:\Python34\python.exe");

            string arg = String.Format("casperjs TESTcasper.js");

            ExecutePythonScript(dir, path, arg, EnvPath);
        }

        private static void ExecutePythonScript(DirectoryInfo workingDir, FileInfo pythonPath, string casperArguments, string EnvPath)
        {
            var p = new Process();
            p.StartInfo.EnvironmentVariables["PATH"] = EnvPath;
            p.StartInfo.WorkingDirectory = workingDir.FullName;
            p.StartInfo.FileName = pythonPath.FullName;
            p.StartInfo.Arguments = casperArguments;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;

            p.ErrorDataReceived += (s, e) =>
            {
                if (!string.IsNullOrEmpty(e.Data))
                    MessageBox.Show("e> " + e.Data);
            };

            p.OutputDataReceived += (s, e) =>
            {
                if (!string.IsNullOrEmpty(e.Data))
                    MessageBox.Show("->" + e.Data);
            };

            p.Start();
            p.BeginOutputReadLine();
            p.BeginErrorReadLine();
            p.WaitForExit();
            p.Close();
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

工作代码: [filepaths C:\ casperjs,C:\ phantomjs&C:\ Python34]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Casper
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        //
            FileInfo info = new FileInfo(@"C:\casperjs\n1k0-casperjs-4f105a9\bin\casperjs");
            DirectoryInfo dir = info.Directory;

            FileInfo path = new FileInfo(@"C:\Python34\python.exe");
            string arg = @"casperjs TESTcasper.js";
            ExecutePythonScript(dir, path, arg);
        }

        private static void ExecutePythonScript(DirectoryInfo workingDir, FileInfo pythonPath, string casperArguments)
        {
            var p = new Process();
            p.StartInfo.WorkingDirectory = workingDir.FullName;
            p.StartInfo.FileName = pythonPath.FullName;
            p.StartInfo.Arguments = casperArguments;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;

            p.ErrorDataReceived += (s, e) =>
            {
                if (!string.IsNullOrEmpty(e.Data))
                    MessageBox.Show("e> " + e.Data);
            };

            p.OutputDataReceived += (s, e) =>
            {
                if (!string.IsNullOrEmpty(e.Data))
                    MessageBox.Show("->" + e.Data);
            };

            p.Start();
            p.BeginOutputReadLine();
            p.BeginErrorReadLine();
            p.WaitForExit();
            p.Close();
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

TESTcasper.js

var casper = require('casper').create();

casper.start('http://casperjs.org/', function() {
    this.echo(this.getTitle());
});

casper.thenOpen('http://phantomjs.org', function() {
    this.echo(this.getTitle());
});

casper.run();
Run Code Online (Sandbox Code Playgroud)

小智 2

成功了。只需确保正确复制所有必需的资源即可。package.json我错过了文件路径中的文件C:\casperjs\n1k0-casperjs-4f105a9

这现在对我有用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Casper
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string Cpath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            //;C:\phantomjs;C:\casperjs\batchbin
            FileInfo csp1 = new FileInfo(Cpath + @"\tools\casperjs\n1k0-casperjs-4f105a9\bin\casperjs");
            FileInfo csp2 = new FileInfo(Cpath + @"\tools\casperjs\n1k0-casperjs-4f105a9\batchbin");
            FileInfo pht = new FileInfo(Cpath + @"\tools\phantomjs\phantomjs-1.9.7-windows\");
            string EnvPath = string.Format(";{0};{1}", pht, csp2);

            DirectoryInfo dir = csp1.Directory;
            FileInfo path = new FileInfo(@"C:\Python34\python.exe");

            string arg = String.Format("casperjs OSTESTcasper.js");
            ExecutePythonScript(dir, path, arg, EnvPath);

        }

        private static void ExecutePythonScript(DirectoryInfo workingDir, FileInfo pythonPath, string casperArguments, string EnvPath)
        {
            var p = new Process();
            p.StartInfo.EnvironmentVariables["PATH"] = EnvPath;
            p.StartInfo.WorkingDirectory = workingDir.FullName;
            p.StartInfo.FileName = pythonPath.FullName;
            p.StartInfo.Arguments = casperArguments;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;

            p.ErrorDataReceived += (s, e) =>
            {
                if (!string.IsNullOrEmpty(e.Data))
                    MessageBox.Show("e> " + e.Data);
            };

            p.OutputDataReceived += (s, e) =>
            {
                if (!string.IsNullOrEmpty(e.Data))
                    MessageBox.Show("->" + e.Data);
            };

            p.Start();
            p.BeginOutputReadLine();
            p.BeginErrorReadLine();
            p.WaitForExit();
            p.Close();
        }

    }

}
Run Code Online (Sandbox Code Playgroud)