2015-09-16 22 views
1
<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)。如何使用Webdriver(Java)獲取下一個隨機生成的父ID

案例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搜索該新行?

謝謝。

+0

「跳到下一位家長(忽略屬於Row00014的所有孩子)」是什麼意思? –

+0

我認爲動態xpath可以幫助你。但爲此,你需要一些先決條件。可以共享任何模式,如Row7d6251f9-f6ea-48bb-90fb-356725307245意味着Row7或Row00007是您點擊或相同的事物或id正在生成沒有任何模式的按鈕 –

+0

什麼是數據Schedule-id,如果你點擊2或3按鈕..是否像數據Schedule-id = 2第二次點擊或其他..其實它是由你的JS完成,所以不能說它是如何發生的 –

回答

0

您可以使用以下定位器

對於病例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')]") 
+0

他並不意味着字面上Row00014 ...只是一個例子。他需要找到第一個隨機生成的ID。 – JeffC

+0

他提到「我可以把Row00014作爲我的基礎參考」,這就是爲什麼我硬編碼它..順便說一句,我剛剛發佈的答案給出了一些想法如何在這種情況下構建xpath,而不是確切的答案。 – Santoshsarma

+0

@JeffC,Santoshsarma,非常感謝。對於案例1,它運作良好。我還在爲案例2工作。不用擔心,我知道Row00014只是一個例子。只是在'By.cssSelector(「tr#row00014 + tr [id * ='Row']」)','tr [id * ='Row']'是否意味着跳到下一個兄弟並忽略孩子?乾杯。 – keylogger

0

從提供的HTML,新行總是有類「新。 「有了這些信息,你可以使用下面的代碼。

WebElement newRow = driver.findElement(By.cssSelector("tr.new")); 
// do something with newRow, e.g. newRow.click(), etc.