Search code examples
seleniumwebdriverhtml-tablegettext

Extracting text from webtable selenium webdriver


HTML:

<tr valign="top"> 
<td> 
  <p align="left"><font face="Arial, Helvetica, sans-serif" size="2"><b><font face="Arial, Helvetica, sans-serif" size="2"><img src="/images/printit.gif" width="57" height="69" align="right"></font><font size="+1">Your 
    itinerary has been booked!</font></b><br>

    <br>
    Please print a copy of this screen for your records. Thank you for choosing 
    Mercury Tours.<br>
    <br>
    </font></p>
</td>
</tr>

I am trying to retrieve the text Your itinerary has been booked! and Please print a copy of this screen for your records. Thank you for choosing Mercury Tours.

I tried with using tagName and xpath to extract the texts and sort it.But is there some unique locator here that could be used here?


Solution

  • According to provided HTML of table row, I'm not sure what could be unique locator. May be, below provided cssSelector would be unique here to extract the text :-

    tr[valign = 'top'] > td > p[align = 'left']
    

    You can use above cssSelector , if it is not unique you need to share more details about table HTML. So I could provide you unique locator.

    Or if you want to locate this element with text, you can use below xpath which would be unique :-

    .//td[contains(.,'itinerary has been booked')]