【问题标题】:How to get the value inside a specific table cell using XPath如何使用 XPath 获取特定表格单元格中的值
【发布时间】:2015-10-28 15:11:47
【问题描述】:

我正在学习如何使用 Behat/Mink 和 Selenium 编写验收测试,我需要验证特定表格单元格的值。我发现我必须使用 xpath 来执行此操作,但我无法弄清楚确切的语法。表格如下:

html:

<table class="admintable generaltable" id="relationships">
<thead>
...
</tr>
</thead>
<tbody><tr class="r0 lastrow">
<td class="leftalign cell c0" style="">Cohort 1</td>
<td class="leftalign cell c1" style="">0</td>
<td class="leftalign cell c2" style="">Non-editing teacher</td>
<td class="centeralign cell c3" style="">No</td>
<td class="leftalign cell c4" style="">No</td>
<td class="leftalign cell c5 lastcol" style=""><a href="http://150.162.242.121/mariana/unasus-cp/local/relationship/edit_cohort.php?relationshipcohortid=1&amp;delete=1"><img src="http://150.162.242.121/mariana/unasus-cp/theme/image.php/standard/core/1445875205/t/delete" alt="Delete" title="Delete" class="iconsmall" /></a> <a href="http://150.162.242.121/mariana/unasus-cp/local/relationship/edit_cohort.php?relationshipcohortid=1"><img src="http://150.162.242.121/mariana/unasus-cp/theme/image.php/standard/core/1445875205/t/edit" alt="Edit" title="Edit" class="iconsmall" /></a></td>
</tr>
</tbody>
</table>

我要写的语句是:

Then I should see "No" in the "???" "css_element"

我应该如何写该语句以指定我想要第一个“否”,在“Inscrição em vários grupos”下,在“否”部分。

编辑:我最初犯了一个错误,我应该使用“xpath_element”作为选择器,而不是“css_element”

【问题讨论】:

    标签: selenium xpath behat acceptance-testing gherkin


    【解决方案1】:

    我假设Inscrição em vários grupos 文本总是出现在第四列。为此编写 XPath 非常简单,因为我们可以直接使用索引。

    //table[@id='relationships']/tbody/tr[1]/td[4]
    

    如果标题列的列号发生变化,您必须找到Inscrição em vários grupos 标题的索引(列号),然后使用它找到对应的值列。

    -- the below code will get you the index of the heading column
    count(//thead//tr/th[.='Inscrição em vários grupo']/preceding-sibling::*)+1
    
    -- use this index to find the corresponding value
    //table[@id='relationships']/tbody/tr[1]/td[count(//thead//tr/th[.='Inscrição em vários grupo']/preceding-sibling::*)+1]
    

    注意:索引从 1 而非 0 开始

    从以下链接来看,behat 中的语法似乎如下:

    How to assert that a text only exists 1 time in Mink

    https://stackoverflow.com/questions/32202047/could-not-find-element-with-xpath-in-behat-mink

    $session = $this->getMainContext()->getSession();
    $element = $session->getPage()->find(
            'xpath',
            $session->getSelectorsHandler()->selectorToXpath('xpath', '//table[@id='relationships']/tbody/tr[1]/td[4]');
    $elementText = $element->getText();
    
    if ($elementText != 'no') {
        throw new Exception('Value us not correct');
    }
    

    【讨论】:

    • 非常感谢您的回复!您建议的xpath返回错误(Css匹配定位器“//table[@id='relationships']/tbody//td[3]”未找到。)位置没有变化,所以我不明白为什么这个定位器不工作
    • 它似乎仍然不起作用。我正在写的步骤是“那么我应该在“//table[@id='relationships']/tbody//tr[4]”“css_element”中看到“No”,对吗?
    • 请检查我的回答。 xpath 是//table[@id='relationships']/tbody/tr[1]/td[4]。这个“css_element”是什么?
    • 这是 Behat 步骤的一部分
    • 我的错,我应该使用“xpath_element”!您对 xpath 的原始建议非常有效!
    【解决方案2】:

    我知道您已经接受了答案,但您是否尝试过像 td.c3 这样的简单 CSS 选择器?这对我来说似乎更直接。

    【讨论】:

    • 我不记得了,但我想我试过了,由于某种原因它不能通过 css 选择器工作。也许还有其他 td 与 c3 作为一个类
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 2011-03-19
    相关资源
    最近更新 更多