【问题标题】:Selecting an XPath class and text of a span element选择跨度元素的 XPath 类和文本
【发布时间】:2013-02-06 01:08:45
【问题描述】:

我有一个这样的元素:

<span class="myTest">Estimates</span>

如何使用 XPath 来选择它?这就是我所拥有的:

x("//*[contains(@class,'myTest')][normalize-space(text())='Estimates']")

【问题讨论】:

  • 这不起作用?您收到错误消息吗?你得到的结果不是你所期望的吗?乍一看,您的 XPath 看起来是合法的(尽管我会用 [normalize-space() = 'Estimates'] 替换第二个谓词,因为它更短且更惯用);怎么了?
  • 嗯我的 casperjs 脚本说它找不到那个元素。我也在尝试这样做: //div[@id='ctl00_cphLeftPanel_mi_estimates_stderrors' and @class='level_2_active']
  • 是的,CasperJS 不能正确突出显示和找到我的元素,是的。
  • span 元素在哪个命名空间中? XHTML?还是根本没有命名空间?

标签: xpath selector casperjs


【解决方案1】:

您的选择器实际上是正确的,这是使用 casperjs master 分支的简短测试:

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

casper.test.begin('your selector is okay', 1, function(test) {
    casper.start().setContent('<span class="myTest">Estimates</span>');
    test.assertExists(x("//*[contains(@class,'myTest')][normalize-space()='Estimates']"));
    test.done();
});

演示:

$ casperjs test test-xpath.js
Test file: test-xpath.js
# your selector is okay
PASS Found an element matching: xpath selector: //*[contains(@class,'myTest')][normalize-space()='Estimates']
PASS 1 tests executed in 0.118s, 1 passed, 0 failed.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 2015-01-03
    • 2018-03-15
    • 2018-05-21
    • 2017-02-24
    • 1970-01-01
    相关资源
    最近更新 更多