没有唯一Id或文本参数的元素的Xpath



我有这个HTML定义,我想点击"搜索"按钮:

<tbody>
<tr>
<td>
<button class="gwt-Button" type="button" style="width: 75px">Search</button>
<td>
</tr>
<tr>
<td>
<button class="gwt-Button" type="button" style="width: 75px">Remove</button>
<td>
</tr>
</tbody>

这里的要点是按钮没有"Text"参数或唯一标识符,只有文本"搜索"或"删除"。我使用robotframework与硒库。已经尝试过(未成功):

点击按钮//按钮[@type = '按钮']/跨度[。= '搜索']

点击按钮//按钮[@type = '按钮']/[。= '搜索']/。

点击按钮//span[contains(text()='Search')]

点击按钮//按钮[@type, ' Button ' and text()='Search']

点击按钮//按钮[@type=' Button ' and contains(.,'Search')]

点击按钮//按钮[@type=' Button ' and span='Search']

点击按钮//html/身体/*[包含(文本()= '搜索'))

点击按钮//跨度[文本()= '搜索']/输入(@type ="radio"

点击按钮//span[text()='Search']

点击按钮//跨度(标签[文本()= '搜索']]/输入(@type ="radio"

点击按钮//按钮[.//text()='Search']

点击按钮//按钮[normalize -space()='Search']

点击按钮//html/body/*[。], '搜索']

点击按钮//按钮[.,'Search']

点击Button//span[.,'Search']

点击按钮//按钮/span[text()='Search']

提前感谢!

如果你的HTML中没有类似的元素,这应该可以工作。

Click Element    xpath=//button[contains(text(), 'Search')]

不好的做法(虽然使用内部文本也是不好的做法-选择更合适的),但您可以使用按顺序搜索:

//tbody/tr[1]//button

最新更新