Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 如何正确使用XPath前置同级_Selenium_Xpath_Selenium Ide - Fatal编程技术网

Selenium 如何正确使用XPath前置同级

Selenium 如何正确使用XPath前置同级,selenium,xpath,selenium-ide,Selenium,Xpath,Selenium Ide,我正在使用Selenium IDE为我的站点编写测试,但我在让Selenium使用前面的兄弟节点单击按钮时遇到了问题 <td> <div class="btn-group"> <button class="btn btn btn-danger block" title="Warning, Delete" name="delete" type="button"> <button class="btn btn btn-default block" title

我正在使用Selenium IDE为我的站点编写测试,但我在让Selenium使用前面的兄弟节点单击按钮时遇到了问题

<td>
<div class="btn-group">
<button class="btn btn btn-danger block" title="Warning, Delete" name="delete" type="button">
<button class="btn btn btn-default block" title="View History" name="history" type="button">
<button class="btn btn btn-default block" title="View Settings" name="settings" type="button">
<button class="btn btn btn-default block" name="device" type="button">
<span class="glyphicon glyphicon-pencil"/>
 Arcade Reader
</button>
</div>
</td>

您不需要升级并使用
,因为所有按钮都位于同一级别:

//button[contains(.,'Arcade Reader')]/preceding-sibling::button[@name='settings']

我还喜欢自上而下构建定位器,如:

//div[contains(@class,'btn-group')][./button[contains(.,'Arcade Reader')]]/button[@name='settings']
这很简单,因为我们只需使用
按钮[contains(,'Arcade Reader')]
搜索
btn组
,就可以得到
按钮[@name='settings']

这只是构建xPath定位器的另一个选项

搜索包装器元素的好处是什么:您可以通过方法返回它(java中的示例),只需构建selenium结构,如:

getGroupByName("Arcade Reader").find("button[name='settings']");
getGroupByName("Arcade Reader").find("button[name='delete']");
甚至更简单

getGroupButton("Arcade Reader", "delete").click();
getGroupButton("Arcade Reader", "delete").click();