Reputation: 135
Want to get text which is outside a tag. Here is the HTML:
<table border="0" cellpadding="0" cellspacing="0" width="100%"
class="viewingsCommentsTbl">
<tbody>
<tr>
<td>
<b style="border: 2px solid red;
background: rgb(204, 136, 136);">Viewing Conducted: </b>
18-May-2016
</td>
</tr>
<tr>
<td style=""><b style="">Duration: </b> 1 hr</td>
</tr>
<tr>
<td style=""><b style="">Comments: </b>66yy</td>
</tr>
</tbody>
</table>
I wanted to get date i.e "18-May-2016"
I tried following XPath, but it does not work:
//*[@class="viewingsCommentsTbl"]/tbody/tr[1]/td/b
Upvotes: 0
Views: 2160
Reputation: 111726
Here is a more robust way to select "18-May-2016"
based on its preceding Viewing Conducted:
label within a td
in the viewingsCommentsTbl
table independent of
table layout:
normalize-space(
substring-after(//table[@class='viewingsCommentsTbl']
//td[starts-with(.,'Viewing Conducted:')],'Viewing Conducted:'))
This gets the text outside of a tag (per your request) by selecting the string value of the element's parent and then using substring-after()
to get just the text that follows the label.
Upvotes: 0
Reputation: 2938
Hi please try it like below
WebElement dateis = driver.findElement(By.xpath("//*[@class='viewingsCommentsTbl']/tbody/tr/td"));
System.out.println("Date is : " + dateis.getText());
and the output is : Date is : Viewing Conducted: 18-May-2016
// also if want to extract date only then
String [] extractdate = dateis.getText().split(" ");
System.out.println("Extracted date is : " + extractdate[2]);
and the output is : Extracted date is : 18-May-2016
Upvotes: 0
Reputation: 50949
The text is in the <td>
tag, not the <b>
. Try
//*[@class="viewingsCommentsTbl"]/tbody/tr[1]/td
Upvotes: 1