2017-07-05 59 views
1

我有一個表,我有幾列,但我想要做的是找到某個實體的編輯鏈接。 的單位名稱和編輯鏈接被確定如下:查找表中的元素 - 硒

@FindBy(css = "div#entity_data_manager_table_cell_name") 
public List<WebElement> entityNameList; 
@FindBy(css = "button#entity_data_manager_table_item_action_edit") 
public List<WebElement> entityEditLinkList; 

什麼是找出對應於某一實體名稱的編輯鏈接的最佳方式?

+1

你可以發佈一些'html'嗎?這種情況的一般方法是使用xpath,因此您可以構造它以查找行項目,並且實質上返回樹狀結構以獲取行並獲取存在於不同列中的關聯編輯按鈕。 – mrfreester

回答

0

解決方案1:

你可以使用正軸或在xpath的祖先。假設有一個tr包含您的實體和編輯按鈕爲實體,你可以這樣做:

//div[@id='entity_data_manager_table_cell_name'][contains(text(),'some identifying text)]/ancestor::tr//button[@id='entity_data_manager_table_item_action_edit'] 

向前軸版本:

//tr[descendant::div[@id='entity_data_manager_table_cell_name'][contains(text(),'some identifying text)]]//button[@id='entity_data_manager_table_item_action_edit'] 

這可能意味着你需要一個動態的選擇,而不是一個@FindBy,所以是這樣的:

protected WebElement getEntityEditButtonElement(string entity) 
{ 
    return driver.findElement(By.xpath(/*xpath from above plugging in the identifiable text*/)); 
} 

解決方案2:

找到您要查找的實體的索引位於entityNameList中,並假設它們匹配,請在您的entityEditLinkList中使用該索引單擊正確的按鈕。這是一個不太精確的,所以我不會推薦它。

+0

我使用了第二種解決方案,迄今爲止它適用於我。謝謝。 –