[英]How to get the next randomly generated parent ID using Webdriver (Java)
<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">
I have this code. 我有这个代码。 Basically I have a table with a lot of rows. 基本上,我的表有很多行。 Using Webdriver (Java) I can click a button and then a new row will be generated with random ID (In this case Row7d6251f9-f6ea-48bb-90fb-356725307245). 使用Webdriver(Java),我可以单击一个按钮,然后将生成带有随机ID的新行(在本例中为Row7d6251f9-f6ea-48bb-90fb-356725307245)。
Case 1: I know that the first row will always be fixed number Row00000. 情况1:我知道第一行将始终是固定编号Row00000。 I can use while loop and increment the number until I don't get Row00015. 我可以使用while循环并递增数字,直到没有得到Row00015。 That means the last fixed row number is Row00014. 这意味着最后一个固定的行号是Row00014。 I can take take Row00014 as my base reference and the next parent is what I want. 我可以将Row00014作为我的基本参考,下一个父母就是我想要的。
How to jump to next parent (ignoring all children belonging to Row00014) and get the newly randomly generated ID ? 如何跳转到下一个父级(忽略属于Row00014的所有子级)并获取新随机生成的ID?
Case 2: In case the table is empty, whenever Webdriver clicks a button, it creates a newly randomly row 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">
In this case, I won't have any base reference. 在这种情况下,我将没有任何基本参考。 How can I search for that new row with dynamic random id ? 如何搜索具有动态随机ID的新行?
Thank You. 谢谢。
You can use below locators 您可以使用以下定位器
For case-1 对于案例1
By.xpath("//tr[@id='Row00014']/following-sibling::tr[1]")
Or
By.cssSelector("tr#Row00014+tr[id*='Row']")
For case-2 对于案例2
By.cssSelector("table[class*='Schedule'] tr[id*='Row']");
Or
By.xpath("//table[contains(@class,'Schedule')]//tr[starts-with(@id,'Row')]")
From the HTML provided, the new row always has the class "new." 在提供的HTML中,新行始终具有“ new”类。 With that info, you can use the code below. 有了该信息,您可以使用下面的代码。
WebElement newRow = driver.findElement(By.cssSelector("tr.new"));
// do something with newRow, e.g. newRow.click(), etc.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.