2012-08-30 67 views
-1

這是我的源代碼我想驗證與硒:硒標籤之間getAttribute標籤?

<tr class="myclass"> 
    <td class="column" style="width:50%"> Key </td> 
    <td style="width:50%"> Value </td> 
    <td style="width:50%"> &nbsp; </td> 
</tr> 

我想驗證他的字「值」包含有。

下面的語句總是返回:而不是「值」

driver.findElement(By.xpath("//tr[td[contains(text(), 'Key')]]/td")).getText(); 

我在做什麼錯誤的「鑰匙」?我如何獲得我的xpath表達式的文本?

回答

1

這裏試試這個

driver.findElement(By.xpath("//tr[@class='myclass']/td[1]")).getText(); 

所有表中的列是兄弟姐妹。如果你在沒有數組的情況下調用它們,它將返回第一個值。如果td [1]不起作用,請嘗試td [2]。

+0

TD [2]的伎倆! – membersound

0

爲什麼不只是這個這樣,它不會依賴於它在位置它只是存在嗎?似乎通過他們全部

//tr[@class='myclass']/td 

,然後循環,確保 '值':

//tr/descendant::td[contains(text(), 'Value')] 

或做一個FindElements。

0

嘗試使用CSS選擇

driver.findElement(By.cssselector(".myclass > td[style='width:50%']")).getText();