2011-03-29 93 views
0

我在我的html页面中有下面的代码。XPath Selector与FirefoxDriver一起正常工作,但不能与HtmlUnitDriver一起工作

<tr id="HL1035569648A" bgcolor="#ffffff"> 

    <td rowspan="2" align="center" width="40"><input name="product_id" value="1035569648" onclick="syncSA(this,document.inquiry.toggleSA);hlChecked(this,document.inquiry.product_id,2,'#ffffff');" type="checkbox"></td> 


    <td rowspan="2" width="90"> <div id="ysop"><a href="http://www.globalsources.com/gsol/I/Induction-cooker/p/sm/1035569648.htm"><img class="imgborder" src="http://akamai.globalsources.com.edgesuite.net/f/593/3445/5d/pdt.static.globalsources.com/IMAGES/PDT/THUMB/648/T1035569648.jpg" alt="Induction Cooker Manufacturers" title="Induction Cooker Manufacturers" onmouseout="showPPOut();" onmouseover="showPPSummary(event,'1035569648',false,rollOvInqURL);" align="left" height="80" hspace="1" vspace="1" width="80"></a> </div></td> 

    <td><a href="http://www.globalsources.com/gsol/I/Induction-cooker/p/sm/1035569648.htm" name="1035569648">Induction <b>Cooker</b></a><br> 
Induction Cooker with 50/60Hz Rated Frequency, 90 to 230V AC Input Voltages and 7.2kW Maximum Power<br></td> 

    <td><a href="http://inductioncooktop.manufacturer.globalsources.com/si/6008838306671/Homepage.htm" title="TOPBAND Induction cookers Division">TOPBAND Induction cookers Division</a><br><a href="http://inductioncooktop.manufacturer.globalsources.com/si/6008838306671/Showroom/3000000149681/ALL.htm">47&nbsp;products</a></td> 


    <td rowspan="2">China (mainland)</td> 
    <td rowspan="2"> 
     <div class="space_filler"><a href="javascript:actionInqSingleInqPath('/gsol/GeneralManager?&amp;catalog_id=2000000003844&amp;design=clean&amp;language=en&amp;action=GetInquireNowBasket&amp;page=inquiry/InqForm&amp;template=RFI&amp;inqflow_path=NLI','InquireNow_KW',1035569648)"><img src="http://akamai.globalsources.com.edgesuite.net/f/593/3445/5d/static.globalsources.com/gsol/en/clean/images/ICON-INQUIRE.GIF" border="0" height="13" width="55"></a></div> 
     <div class="space_filler"><a href="javascript:void(0);" onclick="javascript:checkIM('/gsol/GeneralManager?&amp;catalog_id=2000000003844&amp;design=clean&amp;language=en&amp;page=im/IMSelection&amp;id=6008838306671&amp;IMSource=productsearch',340,455,'pop',null);"><img src="http://akamai.globalsources.com.edgesuite.net/f/593/3445/5d/static.globalsources.com/gsol/en/clean/images/ICON-CHAT.GIF" alt="Chat with this supplier (Login/Registration required)" title="Chat with this supplier (Login/Registration required)" border="0" height="13" width="55"></a></div> 
    </td> 
</tr>` 

我想要检索链接和描述

<td><a href="http://www.globalsources.com/gsol/I/Induction-cooker/p/sm/1035569648.htm" name="1035569648">Induction <b>Cooker</b></a><br> 
Induction Cooker with 50/60Hz Rated Frequency, 90 to 230V AC Input Voltages and 7.2kW Maximum Power<br></td>` 

我使用下面的XPath:

//Link: http://www.globalsources.com/gsol/I/Induction-cooker/p/sm/1035569648.htm 
driver.findElements(By.xpath("//tr[starts-with(@id, 'HL')]/td[3]/a"); 

//Summary: Induction Cooker with 50/60Hz Rated Frequency, 90 to 230V AC Input Voltages and 7.2kW Maximum Power 
driver.findElements(By.xpath("//tr[starts-with(@id, 'HL')]/td[3]"); 

注:我不能用HL1035569648Aid因为有很多表行有这个链接和所有的表行有不同的类名,但他们都以HL开头。

这正常工作与FirefoxDriver,但它不与HtmlUnitDriver工作。有人可以帮助我吗?我不理解这个问题。

谢谢!

+1

你寻求帮助上使用您还没有表现出HTML的XPath。我们无法看到

或元素,因此能够提供有限的帮助。 其次页面使用Javascript来渲染表格吗?如果是这样,我猜你没有在HTMLUnit中打开JavaScript模拟。 – Ardesco2011-03-31 21:58:01

+0

谢谢!我启用了JavaScript,它工作。 – OverrockSTAR 2011-04-02 15:01:28

+0

@Ardesco:更新的问题。 – OverrockSTAR 2011-04-02 21:12:49

回答

0

不惜一切代价避免XPATH定位器!

您可以使用CSS就好:TR [ID^= 'HL'] TD:EQ(2)

但是希望你不需要这个中间位和可以做类似TR [ID^= 'HL']一个

+0

为什么要避免XPath定位器?如果使用正确,它们非常强大。如果你曾经说过“如果你不明白它们是如何工作的,不惜一切代价避免使用XPATH定位器”,那将是有道理的。 – Ardesco 2011-04-11 14:44:22

+0

我不想进入细节,但基本XPATH定位器需要更长的时间硒以定位元素比CSS。例如,对于旧版本的IE,时间差异会变得更大。另外,那些使用XPATH定位器的人往往会无意中创建更脆弱的(特定)定位器。这是最近的硒会议的主题。 – Zugwalt 2011-04-11 16:01:34

+0

我建议同意我对“如果不了解它们如何工作,不惜一切代价避免使用XPATH定位器”的评估。我经常看到很糟糕的XPath,它们搜索整个DOM,而不是在一个特定区域进行目标搜索。这并不意味着XPath不适合作为定位策略,只是大多数人没有花时间学习如何正确使用它,并且对它有所耳闻。 XPath比CSS更准确,90%的案例使用正确且不易碎,不幸的是它们很少被正确使用。 – Ardesco 2011-04-11 21:21:00

0

试试这个:

//tr[contains(@id,"HL")]/td[3]/a 
相关问题