首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用XPath获取特定表单元格内的值

如何使用XPath获取特定表单元格内的值
EN

Stack Overflow用户
提问于 2015-10-28 15:11:47
回答 2查看 8.9K关注 0票数 1

我正在学习如何使用Behat/Mink和Selenium编写验收测试,并且需要验证特定表单元格的值。我发现我必须使用xpath来完成这个任务,但是我无法确定确切的语法。下表如下:

html:

代码语言:javascript
复制
<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>

我想写的声明是:

代码语言:javascript
复制
Then I should see "No" in the "???" "css_element"

我应该如何写这一声明,以具体说明我希望在“否”部分中的“Inscri o em vários grupos”中第一个“否”。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-28 15:55:17

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

代码语言:javascript
复制
//table[@id='relationships']/tbody/tr[1]/td[4]

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

代码语言:javascript
复制
-- 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

代码语言:javascript
复制
$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');
}
票数 2
EN

Stack Overflow用户

发布于 2015-11-08 17:22:07

我知道你已经接受了一个答案,但是你试过像td.c3这样的简单的CSS选择器吗?在我看来更直截了当。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33394754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档