使用java从xpath返回文本?

时间:2017-02-02 11:29:20

标签: string xpath

我想使用xpath为2个元素获取文本,然后将它们连接起来。

By byXpath =(By.xpath("//td[@class='placeholder'/text()][1]");
By byXpat =By.xpath("//td[@class='placeholder2'/text()][1]");

所以我想获得"你好再见"作为一个字符串。

这是我尝试获取元素的xpath:

1.js
" 1"值是因为我有相同的td元素重复几次,我只想要第一个。感谢

2 个答案:

答案 0 :(得分:0)

select mai.msisdn, mai.firstname, mai.lastname, type as account_type,
alias as nickname, mai.regdate, mai.status,
amount as balance, count(trai.referenceid) as number_of_transactions,
sum(trai.amount) as sum_of_transactions from tableA mai
inner join tableC stk
on mai.msisdn = stk.msisdn
inner join tableB trai
on (mai.msisdn = trai.tomsisdn or mai.msisdn = trai.frmsisdn)
where trai.status = 0
and stk.walletid = 0
group by mai.msisdn, mai.firstname, mai.lastname, mai.type,
mai.alias, mai.regdate, mai.status,
stk.amount;

出:

string(//td)

OR:

'\n      hello\n      bye \n  '

出:

normalize-space(//td)

答案 1 :(得分:-1)

您可以使用concat函数来获得所需内容。 xpath是:

concat( (//td/span[@class="placeholder"])[1], " ", (//td/span[@class="placeholder2"])[1] )

假设你有以下xml:

<root>
 <td><span class="placeholder">hello</span>
      <span class="placeholder2">bye</span> 
  </td>
 <td><span class="placeholder">hello2</span>
      <span class="placeholder2">bye2</span> 
  </td>
</root>

xpath选择第一个td/span元素和concats

hello,空格和bye生成hello bye