- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想遍历来自 querySelectorAll 的 NodeList。
不工作
var nodelist = this.evaluate(function() {
return document.querySelectorAll('tr.firstrow');
});
this.echo("nodelist.length=" + nodelist.length);
for (var i=0; i<nodelist.length; i++) {
this.echo("i=" + i);
line = nodelist[i];
this.echo("Line: " + line.innerText);
}
我得到的是这样的:
nodelist.length=3
i=0
this it the first line
i=1
在 i=1 之后,输出卡住。对于第一项,“for”循环按预期运行,但随后不会继续。当我在浏览器控制台中运行 querySelectorAll 时,我看到了所有三个不同的项目。
在这里我发现了一个有效的方法:
有效
var nodelist = this.evaluate(function() {
var nodes = document.querySelectorAll('tr.firstrow');
var array = [nodes[0].innerText,nodes[1].innerText,nodes[2].innerText];
return array;
});
this.echo("nodelist.length=" + nodelist.length);
for (var i=0; i<nodelist.length; i++) {
this.echo("i=" + i);
line = nodelist[i];
this.echo("Line: " + line);
}
但这很不方便。
然后我尝试将这个节点列表转换成一个数组。但是这种方法也行不通,因为评估函数的返回值丢失了。
不工作
var nodelist=this.evaluate(function()
{
console.log("...evaluate()");
var fr_n=document.querySelectorAll('tr.firstrow');
console.log("fr_n.length:" + fr_n.length);
var fr_a=Array.prototype.slice.call(fr_n);
console.log("fr_a.length:" + fr_a.length);
console.log("typeof fr_a:" + typeof fr_a);
console.log("fr_a[0]=" + fr_a[0].innerText);
console.log("fr_a[1]=" + fr_a[1].innerText);
console.log("fr_a[2]=" + fr_a[2].innerText);
return fr_a;
});
this.echo("nodelist.length=" + nodelist.length);
if (Array.isArray(nodelist))
{
this.echo ("nodelist is array");
}
else
{
this.echo ("nodelist is not array");
}
for (var i=0; i<nodelist.length; i++)
{
this.echo("i:" + i);
line = nodelist[i];
this.echo("Line: " + line);
};
产生以下输出:
remote console.log: ...evaluate()
remote console.log: fr_n.length:3
remote console.log: fr_a.length:3
remote console.log: typeof fr_a:object
remote console.log: fr_a[0]=This is the first line I want to see
remote console.log: fr_a[1]=This is the second line I want to see
remote console.log: fr_a[1]=This is the third line I want to see
nodelist.length=0
nodelist is array
[info] [phantom] Done 4 steps in 6943ms
我希望 nodelist.length 是 3 而不是 0。
最佳答案
PhantomJS' page.evaluate()
函数是沙盒页面上下文。传递的所有内容都必须进行本质上的字符串化并再次解析。
Note: The arguments and the return value to the evaluate function must be a simple primitive object. The rule of thumb: if it can be serialized via JSON, then it is fine.
Closures, functions, DOM nodes, etc. will not work!
这意味着您无法从页面上下文中获取元素,但您可以在页面上下文中构建它们的表示并将其传递到外部。
您可能需要这些元素的 textContent
。您可以使用 CasperJS 函数 getElementsInfo()
它为每个元素提供了 text
属性。然后,您可以仅为这一属性过滤信息:
var texts = casper.getElementsInfo('tr.firstrow').map(function(tr){
return tr.text;
});
如果它不是您要查找的文本,则您必须找到另一种表示形式或遍历页面上下文中的 NodeList(在 casper.evaluate()
内)。
关于javascript - 使用 CasperJS 从 querySelectorAll 遍历 NodeList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28509627/
运行测试时获取。 FAIL 35 tests executed in 16.806s, 35 passed, 0 failed, 2 dubious, 0 skipped. “可疑”意味着什么?如何查
是否有人已经使用 casperjs 实现了著名的“页面对象模式”,从长远来看,它对于测试的可维护性非常有用? 当您必须将测试的机制和目的分开时,使用它非常非常酷。以这种方式编写测试会变得更加愉快。 有
所以,我试图在我的服务器上运行 casperJS 作为 cron 作业,这是 crontab: * * * * * /usr/local/bin/casperjs /var/www/javascrip
我知道如何禁用图像和插件,但似乎没有明显的选项来禁用 CasperJS 中的 css。 有谁知道这是如何工作的? 最佳答案 假设您想抑制所有外部样式表的加载,您可以通过中止加载 css 文件的请求来实
我在删除网站时从创建的 URL 下载文件时遇到问题。目前我发现了一个文件的月份和年份,然后替换了 url 中的值并尝试从该位置下载。我了解到您无法使用评估范围内的下载功能。 this.evaluate
在 CasperJS 中,如何在使用 casper.thenOpen() 时保持 session 例如: var casper = require('casper').create(); casper
我希望我的 casper 在 session 期间登录并保持登录状态。有可能吗?如果是这样,如何? 最佳答案 如果你想在 CasperJS 中存储 cookie,你可以使用 PhantomJS coo
我用 casperjs 编写了一些测试。他们与 phantomjs 一起运行得很好。但是,当我尝试通过以下命令使用 slimerjs 时: casperjs --verbose --engine=sl
我用 casperjs 编写了一些测试。他们与 phantomjs 一起运行得很好。但是,当我尝试通过以下命令使用 slimerjs 时: casperjs --verbose --engine=sl
我想单击“提交”按钮,等待下一页加载,然后在第二页上获取html。。我先做然后再运行,但然后一步仍在首页上运行。有任何想法吗? var casper = require('casper').creat
在我的页面自动化脚本中,当我单击标签链接时,将打开一个新窗口。链接的 url 是由复杂的 javascript 生成的。 办理任务 触发后 url 变为: https://oa.phicomm.co
CasperJS(带有 phantomJS)可以直接在浏览器中运行而不是通过命令行运行吗? 我想从浏览器运行交互式测试,我从用户那里获取输入并相应地进行。我想将 casperJS 作为在浏览器中执行的
我正在尝试使用 CasperJS 将图像上传到网络表单。 我的表格看起来像这样: ... Campaign Banner
错误: casper.test property is only available using the `casperjs test` command 在我的整个代码库中搜索了“casper.tes
假设我有一组网址。我不想使用 thenOpen 函数。因为它等待每个以前的 url 被加载并且它减少了加载时间。 casper.each(hrefs,function(self,href){
我正在尝试导航到从脚本本身创建的 url。 此示例代码不像(我曾)预期的那样工作。不知道为什么:( var casper = require('casper').create({ viewpo
我有一个链接列表,我必须模拟这些链接才能使用CasperJS进行点击。他们都共享同一个类(class)。 但是,使用this.click('.click-me')仅单击第一个链接。 单击所有链接的正确
casper.then(function(){ phone_number = '7wqeqwe6'; phone_password = 'Teqweqweqw34'; }); casper.t
我如何告诉 casper 不要加载图像、css、js 视频等。所以我只对 DOM 元素感兴趣。 最佳答案 看看 CasperJS API . var casper = require('casper'
在Phantom中生成PDF时,我可以这样设置纸张大小: page.paperSize = { height: '8.5in', width: '11in', orientation: '
我是一名优秀的程序员,十分优秀!