XPath for text after <br/>

kapperkp picture kapperkp · Apr 19, 2016 · Viewed 10.5k times · Source

Looking to get the XPath of $2.00 with this block:

<td class="undefined" colspan="6">
   <table class="history-bill-payments" cellspacing="0" cellpadding="0" border="0" align="center" width="99%">
<thead>
<tbody>
   <tr>
      <td valign="top">04/19/2016</td>
      <td valign="top" style="text-align:right; height:">
         $3.00
         <br/>
         $2.00
      </td>

I have tried these but to no avail

$I->CanSeeElement("//table[contains(tbody/tr[2]/td/table/tbody/tr/td[2]/following-sibling::br)]"); 
$I->CanSeeElement("//table[contains(tbody/tr[2]/td/table/tbody/tr/td[2]/preceding-sibling::br/text(),'$2.00')]"); 
$I->CanSeeElement("//table[contains(tbody/tr[2]/td/table/tbody/tr/td[2]/following-sibling::br/text(),'$2.00')]");

Using firepath in Firefox I get this XPath

html/body/div[4]/div[2]/div/div/div/div/table/tbody/tr[2]/td/table/tbody/tr/td[2]

I was able to get the xpath of $3.00

$I->CanSeeElement("//table[contains(tbody/tr[2]/td/table/tbody/tr/td[2]/text(),'$3.00')]");

Answer

har07 picture har07 · Apr 19, 2016

In XPath 1.0, given a node-set, contains() would only evaluates the first node in the set. That's why your initial XPath successfully find text node that contains '$3.00', but not the one that contains '$2.00'.

XPath expression that is close to the way your xpath of $3.00 works would be as follow :

//table[tbody/tr[2]/td/table/tbody/tr/td[2]/text()[contains(.,'$2.00')]]

The XPath above works by applying contains() on individual text node instead of passing multiple text nodes at once.