我正在使用 CasperJS 测试我的前端,并且创建了一个名为“Brand New Name”的新元素“foo”,并且已经存在“foo”元素。我需要使用 XPath 选择具有正确名称的特定元素,以便我可以使用 Casper 测试它是否存在。
这是我目前正在做的:
this.test.assertExists(x('//*[@class="foo" and text()="Brand New Name"]'), 'new thing exists')
但这并没有找到我想要的,我做错了什么?
更新 1
这是我收到的 Casper.JS 失败消息,即使 xPath
选择器看起来正确。
FAIL new category exists
# type: assertExists
# file: ../../../foo/test/casper/foo_manager/new_category.js
# subject: false
# selector: {"type":"xpath","path":"//*[@class=\"foo\" and contains(text(), \"Brand New Category\")]"
我已经意识到在我试图选择的元素中实际上有一个span
。我试图解释这一点,但我就是无法让 xPath
CasperJS 选择器工作。我已将 xPath
选择器更新为 contains
,但它仍然无法正常工作。
选择器看起来像这样:
this.test.assertExists(x('//*[@class="foo" and contains(text(), "Brand New Category")]'), 'new category exists')
有问题的元素看起来像这样:
<a href='#' class='foo' id='foo-bar'>
Brand New Category
<span>some text</span>
</a>
最佳答案
如果你尝试这个,你的测试应该通过:
HTML (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CasperJS and XPath</title>
</head>
<body>
<ul>
<li class="foo">Foo</li>
<li class="foo">Bar</li>
<li class="foo">Brand New Name</li>
</ul>
</body>
</html>
JavaScript(xpath.js)
casper.test.begin('CasperJS and XPath', 1, function (test) {
var x = require('casper').selectXPath;
casper.start('index.html', function () {
this.test.assertExists(x('//*[@class="foo" and text()="Brand New Name"]'), 'new thing exists');
});
casper.run(function () {
test.done();
});
});
命令
casperjs test xpath.js
关于javascript - 将 XPath 与 CasperJS 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44290440/