Reputation: 161
Here is my HTML code:
I want to go to the tr where the label is CompanyId and get the td text which is 512571(last line). How can I achive this?
Here is what I tried:
driver.find_element_by_xpath('//label[contains(text(), "CompanyId")]').text
Upvotes: 0
Views: 71
Reputation: 1011
You can go to parent tr
of that label and then direct child node of type td
.
driver.find_element_by_xpath('//label[contains(text(), "CompanyId")]//parent::tr/td').text
That is assuming there is only one td
in that tr
.
Upvotes: 1
Reputation: 247
The following xpath should work:
//th[label[contains(text(),'CompanyId')]]/following-sibling::td
Relevant line of code:
driver.find_element_by_xpath("//th[label[contains(text(),'CompanyId')]]/following-sibling::td").text
Note: by default the xpath will select the first occurrence of a td - in this case CompanyId looks like a label that is immediately followed by the data you are looking for. You could always use an index of you always want the first td
//th[label[contains(text(),'CompanyId')]]/following-sibling::td[1]
Upvotes: 1