【问题标题】:XPath Selector Working fine with FirefoxDriver but not working with HtmlUnitDriverXPath 选择器在 FirefoxDriver 上工作正常,但在 HtmlUnitDriver 上不工作
【发布时间】:2011-03-29 13:06:24
【问题描述】:

我的 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。有没有人可以帮助我。我不明白这个问题。

谢谢!

【问题讨论】:

  • 您正在就使用未显示的 HTML 的 xpath 寻求帮助。我们看不到 或 元素,因此只能提供有限的帮助。其次,页面是否使用 Javascript 来呈现表格?如果是这样,我猜你还没有在 HTMLUnit 中打开 JavaScript 仿真。
  • 谢谢!我启用了 javascript,它起作用了。
  • 还有问题要回答吗?第一条评论说你有它的工作......

标签: testing xpath selenium automation selector


【解决方案1】:

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

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

但是希望你不需要中间的那部分,可以做一些类似 tr[id^='hl'] a

【讨论】:

  • 为什么要避免使用 XPath 定位器?如果使用得当,它们会非常强大。如果您曾说过“如果您不了解 XPATH 定位器的工作原理,则不惜一切代价避免使用它们”,那将是有道理的。
  • 我不想详细介绍,但基本上 XPATH 定位器比 css 定位元素需要更长的时间。例如,对于较旧版本的 IE,时间差异会变得更大。此外,那些使用 XPATH 定位器的人也倾向于无意中创建更脆弱(特定)的定位器。这是最近 Selenium 会议的一个总体主题。
  • 我建议同意我对“如果您不了解 XPATH 定位器的工作原理,不惜一切代价避免使用它们”的评估。我经常看到搜索整个 DOM 而不是在一个特定区域中进行有针对性的搜索的 XPaths。这并不意味着 XPath 作为一种定位器策略不好,只是大多数人没有花时间学习如何正确地执行它并认真对待它。如果使用得当,XPath 在 90% 的情况下比 CSS 更准确且不那么脆弱,不幸的是它们很少被正确使用。
  • 我也同意,如果您在旧版本的 IE 上进行测试,可能值得避免使用 XPath,因为 IE 本身并不支持 XPath,导致您不得不依赖 JavaScript 实现,因为 IE 糟糕的 JavaScript渲染引擎慢得要命,但这适用于任何其他使用 IE 的 JavaScript 渲染引擎的定位器策略。
【解决方案2】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    相关资源
    最近更新 更多