XPath选择器适用于FirefoxDriver,但不能与HtmlUnitDriver一起使用

时间:2011-03-29 13:06:24

标签: testing xpath selenium automation selector

我的html页面中有以下代码。

<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]");

注意:我无法使用HL1035569648A完整id,因为有许多表行具有此链接,并且所有表行都有不同的类名,但它们都以HL开头。< /强>

这适用于FirefoxDriver,但不适用于HtmlUnitDriver。有人可以帮助我吗?我不是在理解这个问题。

谢谢!

2 个答案:

答案 0 :(得分:0)

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

你可以使用css就好了:tr [id ^ ='hl'] td:eq(2)a

然而,希望你不需要那个中间位,可以做一些像tr [id ^ ='hl'] a

答案 1 :(得分:0)

试试这个:

//tr[contains(@id,"HL")]/td[3]/a