gpt4 book ai didi

javascript - 如何使用 XPath 表达式检索 CasperJS 中元素的属性

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:27:12 25 4
gpt4 key购买 nike

我有一个网页,两行之间是这样的:

<a href="http://foo.com/home.do?SID=3443132">...

我需要使用 XPath 提取“href”属性。在 CasperJS 的 API 中写了关于这个的信息:clientutils.getElementByXPath .

这是我的代码:

phantom.casperPath = '..n1k0-casperjs-5428865';
phantom.injectJs(phantom.casperPath + '\\bin\\bootstrap.js');

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

var url = "...";

casper.start(url, function() {
casper.echo("started");
});

var x = require('casper').selectXPath;

casper.then(function()
{
casper.echo("getsid");
this.test.assertExists(x('//a[contains(@href, "home.do?SID=")]'), 'the element exists');
var element = __utils__.getElementByXPath('//a[contains(@href, "home.do?SID=")]');
});

但是失败了。它返回这个:

false
undefined
started
getsid
PASS the element exists <== XPATH WORKS
FAIL ReferenceError: Can't find variable: __utils__
# type: uncaughtError
# error: "ReferenceError: Can't find variable: __utils__"
ReferenceError: Can't find variable: __utils__

最佳答案

试试这个:

phantom.casperPath = '..n1k0-casperjs-5428865';
phantom.injectJs(phantom.casperPath + '\\bin\\bootstrap.js');

var url = "...";
var casper = require('casper').create();
var x = require('casper').selectXPath;

casper.start(url, function() {

casper.echo("started");

});

casper.then(function() {

casper.echo("getsid");

var xpath = '//a[contains(@href, "home.do?SID=")]';
var xpath_arr = { type: 'xpath', path: xpath};

this.test.assertExists(xpath_arr, 'the element exists');

var element = x(xpath);
});

关于javascript - 如何使用 XPath 表达式检索 CasperJS 中元素的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11377487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com