【问题标题】:CasperJS cannot find element even though I can CTRL-F it in Developer Tools即使我可以在开发人员工具中按 CTRL-F,CasperJS 也找不到元素
【发布时间】:2014-12-10 08:42:39
【问题描述】:
'use strict';

var casper = require('casper').create({verbose: true, logLevel: 'debug'});
var element = '/html/body/div[7]/nav/div[5]/div/div/div[2]/div[1]/form';

casper.start('http://store.nike.com/us/en_us/', function () {
    if(this.exists(element)) this.echo('Found it!');
});

casper.run(function() {
    this.exit();
});

好的,所以这段代码 sn-p 尽可能简单。我启动了一个新的 CasperJS 实例,GET http://store.nike.com/us/en_us/,如果找到一个元素则回显,然后退出。现在,我似乎无法理解为什么上面的代码在世界上找不到那个元素(XPath),但是如果我加载 Chrome 开发者工具,我可以 CTRL-F 它。当我用 CSS 路径代替 XPath 时,结果相同。有人可以请教我吗?我错过了什么?

【问题讨论】:

    标签: javascript xpath css-selectors casperjs


    【解决方案1】:

    您需要使用 CasperJS 的 XPath 实用程序,因为 element 是 XPath 表达式,而不是 CSS 选择器。

    var x = require('casper').selectXPath;
    
    casper.start('http://store.nike.com/us/en_us/', function () {
        if(this.exists(x(element))) this.echo('Found it!');
    });
    

    【讨论】:

    • 我刚才确实在测试这个,一切都很好。感谢您的快速回答。
    猜你喜欢
    • 1970-01-01
    • 2020-04-26
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    相关资源
    最近更新 更多