XPath for text after <br/>

為{幸葍}努か 提交于 2019-11-27 07:56:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!