简体   繁体   中英

Not able to locate element using java in selenium Webdriver

I want to select a row and delete it,but id of row is dynamically generated so how can i access it using java in selenium webdriver. when i will add a row by using the application a new tr tag will be added with different id.so how can i select the row.Below is the html code for a table that has two rows with different id's.

<table id="SlotTable" class="noborder" cellspacing="0" cellpadding="0" align="left"     
paging="false" style="border-top: 0px none; table-layout: fixed; width: 984px;">
<tbody id="tableBody">
<script>
<tr id="97.115.104.105.115.104" style="background-color: rgb(221, 221, 221);">
<td width="254px" style="text-overflow: ellipsis; overflow: hidden; white-space: nowrap; width: 254px;">
<td width="110px" style="text-overflow: ellipsis; overflow: hidden; white-space: nowrap; width: 110px;">
<td width="60px" style="text-align: right; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; width: 60px;">
<td width="170px" style="text-align: right; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; width: 170px;">
<td width="100px" style="text-align: right; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; width: 100px;">
<td width="120px" style="text-overflow: ellipsis; overflow: hidden; white-space: nowrap; width: 120px;">
<td width="170 px" style="text-align: right; text-overflow: ellipsis; overflow: hidden; white-space: nowrap;">
</tr>
<tr id="107.117.109.97.114" style="background-color: rgb(232, 232, 232);">
</tbody>
</table>
</div>
</td>
</tr>
<tr>
</tbody>
</table>

You can select the rows for example by using CSS-selectors like :

#SlotTable > tr:nth-child(1)

#SlotTable > tr:nth-child(2)

元素的样式似乎相同,因此您可以将它们用作标识符并选择具有该特定样式的所有元素,然后对这些元素进行操作。

Try to get row using xpath:

//tbody[@id='tableBody']//tr[1]

//tbody[@id='tableBody']/script/tr[1]

Change number to access the row you need.

Put this method you will get list of all the ids of rows and you can perform all the activity after that using their id's :

public ArrayList<String> ListOfIdsOfRows()
{
   WebElement table =driver.findElement(By.id("SlotTable"));
   WebElement tbody=table.findElement(By.tagName("tbody"));
   List<WebElement> rows=tbody.findElements(By.tagName("tr"));
   ArrayList<String> ListOdIds=new ArrayList<>();

   for(int i=0;i<rows.size();i++)
   {
     WebElement row = tbody.findElement(By.xpath("//table[@id='SlotTable']/tbody/tr["+(i+1)+"]"));
     String rowId=row.getAttribute("id");
     ListOdIds.add(rowId);
   }

   return ListOdIds;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM