2017-09-20 17 views
0

在这里,我想提取从下面的代码href值href值,如何提取使用硒在Java

<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>

我曾尝试下面的代码,但它显示了错误

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")); 
+1

什么是错误你有吗? –

回答

0

您可以使用cssSelector insted的的XPath对于这种情况,请尝试使用下面的代码:

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

试试这个XPath:

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