[英]Read a local HTML file from CasperJS start()
I am writing a simple casperjs script to fill a rather complex form on a website. 我正在编写一个简单的casperjs脚本来填充网站上相当复杂的表单。 The HTML code of the website is a bit messy and I don't want to go through the navigation steps to reach the page everytime when I am testing my script. 网站的HTML代码有点乱,我不想在每次测试脚本时都通过导航步骤来访问页面。
I have the form page saved as HTML file but I couldn't even properly load a testing HTML file into casperjs. 我将表单页面保存为HTML文件,但我甚至无法将测试HTML文件正确加载到casperjs中。 Here is the code, file and result: 这是代码,文件和结果:
var casper = require('casper').create();
casper.start('file://test.html').then(function() {
this.echo('started')
this.echo(this.getPageContent())
});
casper.run(function(){
this.echo('ended');
casper.done();
});
The test file: 测试文件:
<html>
<head>
<meta charset="utf-8">
<title>My page</title>
</head>
<body>
<h1 class="page-title">Hello</h1>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
<footer><p>2012 myself</p></footer>
</body>
</html>
The execution result: 执行结果:
C:>started
<html><head></head><body></body></html>
ended
Why the tags within the HTML body are gone? 为什么HTML正文中的标记消失了?
All works fine, with an absolute path: 一切正常,绝对路径:
var casper = require('casper').create();
casper.start('file:///home/root2/pjs/test.html').then(function() {
this.echo('started')
this.echo(this.getPageContent())
});
casper.run(function(){
this.echo('ended');
casper.done();
});
The execution result: 执行结果:
started
<html><head>
<meta charset="utf-8">
<title>My page</title>
</head>
<body>
<h1 class="page-title">Hello</h1>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
<footer><p>2012 myself</p></footer>
</body></html>
ended
You can also try to specify an absolute path like this: 您还可以尝试指定这样的绝对路径:
file:///C://Full/Path/To/test.html
FYI, you can use these functions to get an absolute file uri for a relative one: 仅供参考,您可以使用这些函数获取相对文件的绝对文件uri:
function getAbsoluteFilePath(relativePath) {
return "file:///" + currentDir() + relativePath;
};
// Courtesy https://github.com/casperjs/casperjs/issues/141
function currentDir() {
var sep = "/";
var pathParts = fs.absolute(casper.test.currentTestFile).split(sep);
pathParts.pop();
return pathParts.join(sep) + sep;
}
To use that in your scenario, use this: 要在您的方案中使用它,请使用以下命令:
// ...
casper.start(getAbsoluteFilePath('test.html')).then(function() {
this.echo('started')
this.echo(this.getPageContent())
});
// ...
You can make use of the fs module to get the absolute path of the working directory, and then concatenate the protocol "file://" and the relativePath. 您可以使用fs模块获取工作目录的绝对路径,然后连接协议“file://”和relativePath。
Eg 例如
const fs = require('fs');
var casper = require('casper').create();
// your casper logic here
console.log(getAbsoluteFilePath('test.html'));
casper.run();
function getAbsoluteFilePath(relativePath) {
return "file://" + fs.workingDirectory + '/' + relativePath;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.