即使我使用正确的className,我也无法捕获所有搜索结果链接和描述

时间:2011-03-25 21:14:22

标签: xpath selenium automation selector

我想从结果页面捕获所有搜索结果链接(搜索引擎:http://search.yahoo.com)和摘要。链接的ClassName为'yschttl spt',摘要的className为 'abstr'

源代码如下所示,链接和摘要。

链接:

<a id="yui_3_3_0_1_1301085039901361" dirtyhref="http://search.yahoo.com/r/_ylt=A0oG7m9u.4xNvWYA7N5XNyoA;_ylu=X3oDMTE2ZXNhNjRzBHNlYwNzcgRwb3MDMgRjb2xvA2FjMgR2dGlkA01TWUMwMDFfMTc5/SIG=11stois8r/EXP=1301106638/**http%3a//en.wikipedia.org/wiki/Pune,_India" class="yschttl spt" href="http://search.yahoo.com/r/_ylt=A0oG7m9u.4xNvWYA7N5XNyoA;_ylu=X3oDMTE2ZXNhNjRzBHNlYwNzcgRwb3MDMgRjb2xvA2FjMgR2dGlkA01TWUMwMDFfMTc5/SIG=11stois8r/EXP=1301106638/**http%3a//en.wikipedia.org/wiki/Pune,_India" data-bns="API" data-bk="5096.1"><b>Pune</b> - Wikipedia, the free encyclopedia</a>`

摘要Div:

<div id="yui_3_3_0_1_1301085039901338" class="abstr"><b id="yui_3_3_0_1_1301085039901337">Pune</b> is undoubtedly a great place to eat. Fergusson <b id="yui_3_3_0_1_1301085039901352">College</b> <b>Road</b> is full of budget eateries serving delicous hot food at nominal charges. For a range of multi-cuisine ...</div>

我使用下面的代码行捕获(链接和摘要)。

final List<WebElement> links = driver.findElements(By.className("yschttl spt")); 
final List<WebElement> linksSummary = driver.findElements(By.className("abstr"));

但现在正在努力。

我也尝试在下面的XPath中使用Link:

//a[starts-with(@id, 'yui_')]

//a[@data-bns='API']

我无法使用ID作为整体,因为所有搜索结果链接的ID号都不相同。

什么都没有用。请帮忙。

提前感谢。

2 个答案:

答案 0 :(得分:2)

主要问题是你没有正确地要求一个类名。 class="yschttl spt"的实例表示该元素可能由两个类名称yschttlspt标识。 表示类名为yschttl spt,因此要求By.className("yschttl spt")将始终失败。

请注意@Tarun建议的XPath工作的原因是XPath具有 no 概念,即HTML类名称应该是什么或应该是什么。在XPath中,@class只是指定属性的名称 - 使用基础语义。

此外,请注意,类名可能不包含空格。有关指定类名的更多详细信息,请参阅HTML class属性规范。

答案 1 :(得分:0)

我不使用selenium 2.0但是当我在Firefox XPath Checker中尝试//a[@class='yschttl spt']时,我在页面中看到了所有10个结果。我很快就会开始使用Selenium 2.0,也许我可以试试......