问题
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')]");
回答1:
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.
回答2:
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())
回答3:
If you need, just add table id or any other specific locator.
xpath=//table//tr/td[2]/text()[2]
来源:https://stackoverflow.com/questions/36718817/xpath-for-text-after-br