2016-03-30 30 views
3

如果有人需要參考或背景這裏是我的第一個問題問WebElement名單召喚單項指標

Retrieving a list of WebElements and Identifying them

此時我已取回

@FindBy(css = "td[id^=ctl00_SomeGridData_ucControlList_trgControlList_ctl00__]") 
List<WebElement> allGridData; 

WebElements列表此時在我的代碼中,我可以使用web元素來調用索引,例如

allGridData.get(0).click 

但是名單不嚴格,例如整數,如果我行級<tr>訪問這將是:

ctl00_SomeGridData_ucControlList_trgControlList_ctl00__0 

但如果我是叫行內的鏈接,他們是表數據<td>分成div的,看起來像這樣的:

ctl00_SomeGridData_ucControlList_trgControlList_ctl00__ctl04_lbView 
ctl00_SomeGridData_ucControlList_trgControlList_ctl00__ctl04_hlTestPlan 

或本

ctl00_SomeGridData_ucControlList_trgControlList_ctl00__ctl07_lbView 
ctl00_SomeGridData_ucControlList_trgControlList_ctl00__ctl07_hlTestPlan 

由於所有的WebElements有一個共同的CSS選擇器開始

@FindBy(css =  "td[id^=ctl00_SomeGridData_ucControlList_trgControlList_ctl00__]") 
    List<WebElement> allGridData; 

如何識別一個特定的索引是一個char值(即ctl107)vs只是一個整數?

+0

您可以發佈您正在使用的HTML代碼嗎?將事情弄清楚。 – alecxe

+0

已在頂部更新 –

回答

2

假設你想兩個列表,一個用於查看詳細信息,一個用於查看測試計劃,你需要的$(結尾):

@FindBy(css = "a[id$=lbView]") 
List<WebElement> allDetailViewLinks; 

@FindBy(css = "a[id$=hlTestPlan]") 
List<WebElement> allTestPlanLinks; 

但我最好的猜測是,你要點擊一個特定行中的鏈接,而不是基於Web元素列表中的索引。例如根據td中的文字<tr id="ctl00_SoxMain_ucControlList_trgControlList_ctl00__0" class="rgRow"> <td class="rgExpandCol" valign="top"/><td valign="top">AL-01</td>

您需要一種方法來獲取td中具有特定文本的行。

WebElement getRow(String specificValue) { 
    return driver.findElement(By.xpath("//td[text()='"+specificValue+"']")) 
      .findElement(By.xpath("..")); 
} 

然後,您可以製作詳細視圖和測試計劃視圖的方法。

public void openDetailsView(String specificValue) { 
    getRow(specificValue) 
     .findElement(By.cssSelector("a[id$=lbView]")) 
     .click(); 
} 

public void openTestPlanView(String specificValue) { 
    getRow(specificValue) 
     .findElement(By.cssSelector("a[id$=hlTestPlan]")) 
     .click(); 
} 
+0

非常感謝您的詳細解釋!我會得到這個實施。 –

+0

沒問題,祝你好運! –