<table class="ui-table Schedule detailed">
<thead>
<tbody>
<tr id="Row00000" class="Schedule-row 0" data-Schedule-id="0">
<tr id="Row00001" class="Schedule-row 0 odd" data-Schedule-id="0">
<tr id="Row00002" class="Schedule-row 0" data-Schedule-id="0">
<tr id="Row00003" class="Schedule-row 0 odd" data-Schedule-id="0">
<tr id="Row00004" class="Schedule-row 0" data-Schedule-id="0">
<tr id="Row00005" class="Schedule-row 82 odd" data-Schedule-id="82">
<tr id="Row00006" class="Schedule-row 80" data-Schedule-id="80">
<tr id="Row00007" class="Schedule-row 79 odd" data-Schedule-id="79">
<tr id="Row00008" class="Schedule-row 0" data-Schedule-id="0">
<tr id="Row00009" class="Schedule-row 0 odd" data-Schedule-id="0">
<tr id="Row00010" class="Schedule-row 0" data-Schedule-id="0">
<tr id="Row00011" class="Schedule-row 0 odd" data-Schedule-id="0">
<tr id="Row00012" class="Schedule-row 0" data-Schedule-id="0">
<tr id="Row00013" class="Schedule-row 0 odd" data-Schedule-id="0">
<tr id="Row00014" class="Schedule-row 0" data-Schedule-id="0">
<td class="hidden col-id-personNo" data-col-id="personNo"/>
<td class="active w130 col-id-TsDate indicate first yellow" data-col-id="TsDate">
<td class="hidden w100 col-id-StartTime" data-col-id="StartTime"/>
<td class="active col-id-TaskId" data-col-id="TaskId"/>
<td class="hidden col-id-OriginalKey" data-col-id="OriginalKey"/>
<td class="hidden col-id-ScheduleId" data-col-id="ScheduleId"/>
<td class="hidden col-id-TsStatus" data-col-id="TsStatus"/>
<td class="hidden col-id-Quantity" data-col-id="Quantity"/>
<td class="active check w25 col-id-Selected" data-col-id="Selected"/>
<td class="active buttons col-id-RowButton" data-col-id="RowButton"/>
</tr>
<tr id="Row7d6251f9-f6ea-48bb-90fb-356725307245" class="Schedule-row -1 new odd" data-Schedule-id="-1">
我有这个代码。基本上我有一个有很多行的表。使用 Webdriver (Java),我可以单击一个按钮,然后将生成一个具有随机 ID 的新行(在本例中为 Row7d6251f9-f6ea-48bb-90fb-356725307245)。
情况 1: 我知道第一行始终是固定数字 Row00000。我可以使用 while 循环并递增数字,直到没有得到 Row00015。这意味着最后一个固定行号是 Row00014。我可以将 Row00014 作为我的基本引用,下一个父级就是我想要的。
如何跳转到下一个父级(忽略属于 Row00014 的所有子级)并获取新随机生成的 ID?
情况 2: 如果表为空,每当 Webdriver 单击按钮时,它都会创建一个新的随机行 ID。
<table class="ui-table Schedule detailed">
<thead>
<tbody>
<tr id="Row7d6251f9-f6ea-48bb-90fb-356725307245" class="Schedule-row -1 new odd" data-Schedule-id="-1">
在这种情况下,我不会有任何基础引用。如何使用动态随机 id 搜索新行?
谢谢。
最佳答案
您可以使用以下定位器
对于案例 1
By.xpath("//tr[@id='Row00014']/following-sibling::tr[1]")
Or
By.cssSelector("tr#Row00014+tr[id*='Row']")
对于案例 2
By.cssSelector("table[class*='Schedule'] tr[id*='Row']");
Or
By.xpath("//table[contains(@class,'Schedule')]//tr[starts-with(@id,'Row')]")
关于java - 如何使用 Webdriver (Java) 获取下一个随机生成的父 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32600432/