如何正确使用XPath previous-sibling

时间:2022-11-27 07:35:43

I am writing tests for my site using Selenium IDE and I am having trouble with having selenium click on a button using preceding-sibling

我正在使用Selenium IDE为我的网站编写测试,而我在使用previous-sibling点击按钮时遇到了麻烦

<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>

My path

我自己的路

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

2 个解决方案

#1


35  

You don't need to go level up and use .. since all buttons are on the same level:

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

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

#2


2  

i also like to build locators from up to bottom like:

我也想建立从上到下的定位器,如:

//div[contains(@class,'btn-group')][./button[contains(.,'Arcade Reader')]]/button[@name='settings']

it's pretty simple, as we just search btn-group with button[contains(.,'Arcade Reader')] and gets it's button[@name='settings']

这很简单,因为我们只用按钮[contains(。,'Arcade Reader')搜索btn-group并获取它的按钮[@ name ='settings']

that's just another option to build xPath locators)

这只是构建xPath定位器的另一种选择)

what is the profit of searching wrapper element: you can return it by method (example in java) and just build selenium constructions like:

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

getGroupByName("Arcade Reader").find("button[name='settings']");
getGroupByName("Arcade Reader").find("button[name='delete']");

or even simplify more

甚至简化更多

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

#1


35  

You don't need to go level up and use .. since all buttons are on the same level:

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

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

#2


2  

i also like to build locators from up to bottom like:

我也想建立从上到下的定位器,如:

//div[contains(@class,'btn-group')][./button[contains(.,'Arcade Reader')]]/button[@name='settings']

it's pretty simple, as we just search btn-group with button[contains(.,'Arcade Reader')] and gets it's button[@name='settings']

这很简单,因为我们只用按钮[contains(。,'Arcade Reader')搜索btn-group并获取它的按钮[@ name ='settings']

that's just another option to build xPath locators)

这只是构建xPath定位器的另一种选择)

what is the profit of searching wrapper element: you can return it by method (example in java) and just build selenium constructions like:

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

getGroupByName("Arcade Reader").find("button[name='settings']");
getGroupByName("Arcade Reader").find("button[name='delete']");

or even simplify more

甚至简化更多

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