如何在java中使用selenium提取href值

时间:2017-09-20 07:45:40

标签: java selenium

这里我想从下面的代码中提取href值,



<table id="offers_table" class="fixed offers breakword" summary="" width="100%" cellspacing="0">
<tbody>
<tr>
<tr>
<tr>
<td class="offer onclick ">
<table class="fixed breakword ad_id1ezENl" summary="Ad" data-photos="2" width="100%" cellspacing="0">
<tbody>
<tr>
<td rowspan="2" width="164">
<div class="space">
<span class="rel inlblk detailcloudbox">
<a class="thumb vtop inlblk rel tdnone linkWithHash scale5 detailsLink" 
href="https://www.olx.in/item/hyundai-accent-car-ID1ezENl.html#1a86c09693" title="">
</span>
</div>
</td>
<td valign="top">
<td class="wwnormal tright td-price" width="170" valign="top">
</tr>
<tr>
</tbody>
</table>
&#13;
&#13;
&#13;

我在下面的代码中尝试过,但它显示错误

WebElement ele=driver.findElement(By.id("offers_table"));
  WebElement href=ele.findElement(By.xpath("//tr[3]/span[@class='rel inlblk detailcloudbox']/a[@href]"));
  System.out.println(href.getAttribute("href"));

2 个答案:

答案 0 :(得分:0)

试试这个xpath:

//a[@class='thumb vtop inlblk rel tdnone linkWithHash scale5 detailsLink']
[@href='https://www.olx.in/item/hyundai-accent-car-ID1ezENl.html#1a86c09693']

答案 1 :(得分:0)

对于这种情况,你可以使用xpath的cssSelector,尝试使用下面的代码:

 String hrefvalue = driver.findElement(By.cssSelector("span.rel.inlblk.detailcloudbox > a")).getAttribute("href");