同一页面中重复字段名称的Xpath解决方案

时间:2014-07-22 10:00:41

标签: selenium xpath selenium-webdriver

网页中有三个具有动态ID的保存按钮。指导我如何定义最佳xpath,目前我已经应用了完整的xpath起始html节点。

首先保存按钮HTML代码

<button id="j_idt131" name="j_idt131" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only button" onclick="PrimeFaces.ab({source:'j_idt131',update:'csqa_form',oncomplete:function(xhr,status,args){if (args &amp;&amp; !args.validationFailed) qaChangeDialog.show();}});return false;" type="submit" role="button" aria-disabled="false"><span class="ui-button-text ui-c">Save</span></button>

第二个保存按钮HTML代码

<button id="j_idt104" name="j_idt104" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only button" onclick="PrimeFaces.ab({source:'j_idt104',update:'cemail_form',oncomplete:function(xhr,status,args){if (args &amp;&amp; !args.validationFailed) emailChangeDialog.show();}});return false;" type="submit" role="button" aria-disabled="false"><span class="ui-button-text ui-c">Save</span></button>

第三个保存按钮HTML代码

<button id="j_idt82" name="j_idt82" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only button" onclick="PrimeFaces.ab({source:'j_idt82',update:'cpwd_form',oncomplete:function(xhr,status,args){if (args &amp;&amp; !args.validationFailed) pwdChangeDialog.show();}});return false;" type="submit" role="button" aria-disabled="false"><span class="ui-button-text ui-c">Save</span></button>

enter image description here

2 个答案:

答案 0 :(得分:1)

由于你知道按钮的顺序,你可以试试这个xpath,

//button[contains(@id,"j_idt")][1]
//button[contains(@id,"j_idt")][2] 
//button[contains(@id,"j_idt")][3] 

答案 1 :(得分:0)

您可以通过贴纸链接提供整页标记 我估计您可以通过选择//标签[。=&#39; Answert3&#39;] / follow :: button [。=&#39; Save&#39;]

来使用AxisName