2016-11-03 88 views
0

這裏是我的HTML代碼:無法找到的XPath

enter image description here

我想去的地方TR標籤是CompanyId並獲得TD文本是512571(最後一行)。我怎樣才能做到這一點?

這裏是我的嘗試:

driver.find_element_by_xpath( '//標籤[包含(文本(), 「CompanyId」)')文本

+0

我不想使用標識 – user5653362

回答

1

以下XPath應工作:

//th[label[contains(text(),'CompanyId')]]/following-sibling::td 

相關的代碼行:

driver.find_element_by_xpath("//th[label[contains(text(),'CompanyId')]]/following-sibling::td").text 

注:默認的XPath將選擇TD的第一次出現 - 在這種情況下CompanyId看起來就像一個標籤,緊跟着你正在尋找的數據。你總是可以使用索引的你總是想第一個TD

//th[label[contains(text(),'CompanyId')]]/following-sibling::td[1] 
1

你可以去的那個父tr。標籤然後直接輸入td類型的子節點。

driver.find_element_by_xpath('//label[contains(text(), "CompanyId")]//parent::tr/td').text 

這是假設只有一個tdtr

+0

這是給 - 提高exception_class(消息,屏幕,堆棧跟蹤) selenium.common.exceptions.NoSuchElementException:消息:無此元素:無法找到元素:{「method」:「xpath」,「selector」:「// label [contains(text(),」CompanyId「)]/parent :: tr/td」} – user5653362

+0

父母之前只有一個td – user5653362

+0

嘗試使用雙斜線。更新的答案。 – falloutcoder