Bor*_*ris 5 selenium node.js browserstack
抱歉这么愚蠢的问题,
我怎样才能在NodeJS中通过字符串从文件字符串中读取一些值,例如 - url,并最终对每个字符串进行操作?
var contents = fs.readFileSync('test.txt', 'utf8');
Run Code Online (Sandbox Code Playgroud)
什么呢?
它需要browserstack + selenium测试.我想从文件中逐个运行一些链接并对它们做一些事情.
更改了以下代码:来自
console.log(lines[i++])
Run Code Online (Sandbox Code Playgroud)
至
line = (lines[i++])
driver.get(line);
driver.getCurrentUrl()
.then(function(currentUrl) {
console.log(currentUrl);
Run Code Online (Sandbox Code Playgroud)
但它工作一次.
和
var str=fs.readFileSync('test.txt');
str.split(/\n/).forEach(function(line){})
C:\nodejstest>node test1.js
C:\nodejstest\test1.js:57
str.split(/\n/).forEach(function(line){
^
TypeError: str.split is not a function
at Object.<anonymous> (C:\nodejstest\test1.js:57:5)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:142:18)
at node.js:939:3
Run Code Online (Sandbox Code Playgroud)
作品!太多了!
另一种(更简单的)方法是将整个文件读入缓冲区,将其转换为字符串,在行终止符上拆分字符串以生成行数组,然后遍历数组,如下所示:
var buf=fs.readFileSync(filepath);
buf.toString().split(/\n/).forEach(function(line){
// do something here with each line
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4500 次 |
| 最近记录: |