2013-02-06 101 views
1

的XPath类和文字我有像这样的元素:选择span元素

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

如何使用XPath来选择呢?这是我有:

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

,这是不工作?你有错误信息吗?你得到的结果是不是你所期望的?你的XPath看起来合法乍一看(尽管我会用'[normalize-space()='Estimates']'替换第二个谓词,因为它更短,更习惯。怎么了? –

+0

嗯,我的casperjs脚本说它找不到那个元素。我也想这样做:// div [@ id ='ctl00_cphLeftPanel_mi_estimates_stderrors'and @ class ='level_2_active'] – cdub

+0

是的,必须对CasperJS有些事情没有正确地突出显示并找到我的元素。 – cdub

回答

1

你的选择其实是正确的,这是一个使用casperjs掌握分支一个简短的测试:

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.