动态元素处理

时间:2017-04-17 10:43:46

标签: selenium selenium-webdriver dynamic automation

在我们的应用程序中有一个任务视图页面。对于不同的任务,发现相同按钮的xpath每次都会发生变化,如下所示

WIN_1_1000000014  
WIN_2_1000000014  
WIN_3_1000000014  
WIN_4_1000000014  
WIN_5_1000000014  

I am providing html codes for two of such scenarios below . I am using following xpaths to find that button but not working for me. 

// div [@ardbn ='受让人组织'] // [@class =' btn btn3d菜单'] // img
  // img [@alt ='组织菜单']

帮帮我们



<div id="WIN_1_1000000014" class="df arfid1000000014 ardbnAssigneeOrganization Char" arid="1000000014" artype="Char" ardbn="Assignee Organization" arlbox="0,4,78,17" ardcf="1" style="z-index:998;top:84px; left:5px; width:263px; height:21px;" arwindowid="1">
  <label id="label1000000014" class="label f6" for="x-arid_WIN_1_1000000014" style="top: 4px; left: 0px; width: 78px; height: 17px;">Organization</label>
  <textarea id="arid_WIN_1_1000000014" class="text sr " cols="20" maxlen="60" style="top:0px; left:83px; width:154px; height:21px;" armenu="CTM:SGP:SupportOrganization3-Q" mstyle="2" arautoc="1" arautocmb="1" arautocak="0" arautoctt="400" rows="1" title="Service_Desk_First_Tier"
    wrap="off"></textarea>
  <a class="btn btn3d menu" href="javascript:" style="top:0px; left:242px; width:21px; height:21px;">
    <img class="btnimg" src="../../../../resources/images/mt_sprites.gif" alt="Menu for Organization" title="" style="background-color: transparent;">
  </a>
&#13;
&#13;
&#13;

&#13;
&#13;
<div id="WIN_3_1000000014" class="df arfid1000000014 ardbnAssigneeOrganization Char" arid="1000000014" artype="Char" ardbn="Assignee Organization" arlbox="0,4,78,17" ardcf="1" style="z-index:998;top:84px; left:5px; width:263px; height:21px;" arwindowid="3">
  <label id="label1000000014" class="label f6" for="x-arid_WIN_3_1000000014" style="top: 4px; left: 0px; width: 78px; height: 17px;">Organization</label>
  <textarea id="arid_WIN_3_1000000014" class="text sr " cols="20" maxlen="60" style="top:0px; left:83px; width:154px; height:21px;" armenu="CTM:SGP:SupportOrganization3-Q" mstyle="2" arautoc="1" arautocmb="1" arautocak="0" arautoctt="400" rows="1" title="Service_Desk_Resolver"
    wrap="off"></textarea>
  <a class="btn btn3d menu" href="javascript:" style="top:0px; left:242px; width:21px; height:21px;">
    <img class="btnimg" src="../../../../resources/images/mt_sprites.gif" alt="Menu for Organization" title="" style="background-color: transparent;">
  </a>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

您可以编写最新任务的xpath,如下所示:

(// div [contains(@ id,'WIN _')])[last()] // img [@ alt ='组织菜单']

答案 1 :(得分:1)

使用以下代码进行点击或任何其他操作:

List<WebElement> elements = driver.findElements(By.xpath("//img[@alt='Menu for Organization']"));

        for(int i=1;i<=elements.size();++i){
 elements.get(i).click();
        }

答案 2 :(得分:0)

为前3个元素工作的xpath是
(// div [@ ardbn ='Assignee Organization'] //一个[@class ='btn btn3d menu'] // img)[last()]
为休息元素工作的xpath是
(// div [@ ardbn ='Assignee Organization'] //一个[@class ='btn btn3d menu'] // img)[position()&lt; 3]