Xpath CasperJS,取最后一个元素

Xpath CasperJS, take last element

我的 Xpath 有问题。

我有那个:

<ul unselectable="on" class="k-list k-reset" tabindex="-1" role="listbox" aria-hidden="true" id="1 (fix but random)" aria-live="off" style="overflow: auto;">
<li tabindex="-1" role="option" unselectable="on" class="k-item"></li>
<li tabindex="-1" role="option" unselectable="on" class="k-item">standard 2014</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item k-state-selected k-state-focused" id="1 (fix but random)__PackageId_option_selected" aria-selected="true">standard couple 2014</li><li tabindex="-1" role="option" unselectable="on" class="k-item">Gratuit</li>
</ul>

<ul unselectable="on" class="k-list k-reset" tabindex="-1" role="listbox" aria-hidden="true" id="2 (fix but random)" aria-live="off" style="overflow: auto;">
<li tabindex="-1" role="option" unselectable="on" class="k-item"></li>
<li tabindex="-1" role="option" unselectable="on" class="k-item">standard 2014</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item k-state-selected k-state-focused" id="1 (fix but random)__PackageId_option_selected" aria-selected="true">standard couple 2014</li><li tabindex="-1" role="option" unselectable="on" class="k-item">Gratuit</li>
</ul>

<ul unselectable="on" class="k-list k-reset" tabindex="-1" role="listbox" aria-hidden="true" id="RANDOM" aria-live="off" style="overflow: auto;">
<li tabindex="-1" role="option" unselectable="on" class="k-item"></li>
<li tabindex="-1" role="option" unselectable="on" class="k-item">standard 2014</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item k-state-selected k-state-focused" id="1 RANDOM__PackageId_option_selected" aria-selected="true">standard couple 2014</li><li tabindex="-1" role="option" unselectable="on" class="k-item">Gratuit</li>
</ul>

我的问题很简单:如何使用 standard 2014 在 last

  • 上用 casper 单击? 我试试看:

    var x = require('casper').selectXPath;
    this.click(x('//*ul[@class="k-list k-reset"]/li[text() = "standard 2014"]'))
    

    但是casperjs没有找到。 感谢您的帮助!

  • 尝试将您的 xpath 括在括号中并在末尾添加 [last()]。喜欢(xpath)[last()]

    您还应该从 //*ul

    中删除星号

    它应该是这样的:

    this.click(x('(//ul[@class="k-list k-reset"]/li[text() = "standard 2014"])[last()]')) 
    

    您应该尝试在浏览器的控制台中检查您的 X 路径是否有效。喜欢

    $x('//a[contains(text(),"Cancel")]')