11

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')]");
kjhughes
  • 106,133
  • 27
  • 181
  • 240
kapperkp
  • 199
  • 1
  • 3
  • 14

3 Answers3

7

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.

har07
  • 88,338
  • 12
  • 84
  • 137
6

td with certain contents

From your trials, it seems you're fine with keying off of $2.00 literally, so you could use this XPath 2.0 expression to get the td that ends with $2.00:

//td[ends-with(normalize-space(), '$2.00')]

Note that browsers don't generally support XPath 2.0, so use this XPath 1.0 expression if running within a browser and you're ok with $2.00 appearing anywhere within the td:

//td[contains(.,'$2.00')]

Text following a br

If you don't want to literally specify the $2.00, you'll have to state some other invariant constraint. For example, this XPath will return the string that follows the br contained within a td that starts with $3.00:

normalize-space(//td[starts-with(normalize-space(),'$3.00')]/br/following::text())

See also

kjhughes
  • 106,133
  • 27
  • 181
  • 240
4

If you need, just add table id or any other specific locator.

xpath=//table//tr/td[2]/text()[2]
Tomas Chudjak
  • 562
  • 3
  • 11
  • Ok, somebody downvoted working xpath, so I guess the question is looking exactly for '$2.00', thus a bit of improvement - xpath=//table//tr/td[2]/text()[2 and contains(.,'$2.00')] but still it's pretty simple and working – Tomas Chudjak Apr 19 '16 at 13:47
  • i tried your answer and got malrformed xpath error see link http://prntscr.com/au6jk8 – kapperkp Apr 19 '16 at 14:08
  • removed the extra / before the tr and it worked but couldnt find the $2.00 – kapperkp Apr 19 '16 at 14:15
  • ok. probably the code structure is wider than in the given example. In the initial xpaths there are multiple table elements. We need to specify locator a bit more. With @class attribute, you can find the closest parent. if you want to try xpath=//table[@class='history-bill-payments']//tr/td[2]/text()[2] .. But anyway, the main point with the selection wast the last part /text()[2] – Tomas Chudjak Apr 19 '16 at 14:30