使用相对xpath和文本描述符

时间:2017-01-26 02:50:23

标签: robotframework selenium2library

我有一个测试场景,我在表中创建一个新条目(Banana),然后修改该条目,然后删除它。

该表在新条目的文本描述旁边有一个修改按钮和一个删除按钮。我想使用相对xpath来定位文本值,然后根据正在执行的测试场景以某种方式选择删除或修改按钮。 enter image description here

正在使用的代码示例是:

*** Settings ***
[Documentation]  Delete Fruit from table
Suite Teardown  Close all browsers
Library  Selenium2Library
Library  XvfbRobot
Library  Collections

*** Variables ***
${delFruit}  Banana

*** Test Cases ***
Delete Fruit Button
    wait until element is visible  xpath=//div[@id='${delFruit}']
    click element  xpath=//div[@id='${delFruit}']/a[2]/span
    confirm action

以下是幕后html的片段 - 所有删除按钮都使用"删除水果"的文本描述符:

 <a class="button micro primary error" onclick="deleteFruit(3)" href="javascript:void(0)">
    <span class="fa fa-trash" title="Delete Fruit" border="0" align="absmiddle"></span>
    </a>
    </td>
    <td class="cell ">Banana</td>

问题是,当我创建表的新条目时,表内容按字母顺序修改。所以实际上,香蕉入口是在Apple和Orange之间。

我可以将click元素操作硬编码到删除按钮: click element xpath=/html/body/div[1]/div/div[2]/div/div[2]/table/tbody/tr[1]/td[1]/a[2]

我希望找到一种方法来识别Banana xpath=/html/body/div[1]/div/div[2]/div/div[2]/table/tbody/tr[1]/td[1]/a[2]左侧的元素,因为随着新项目的添加,表项的位置会发生变化。

有没有人对如何选择香蕉左边的删除按钮有任何建议?

1 个答案:

答案 0 :(得分:1)

在了解您的情况时,prescending-sibling xpath轴应该有所帮助:

//td[contains(text(), 'Banana')]/prescending-sibling::td/a[/span[contains(@title, 'Delete')]]