开发者

I am not able to capture all search result links and description even though i am using correct className

I want to capture all search result links (search engine: http://search.yahoo.com) and summary from the result page. ClassName of Link is 'yschttl spt' and className for summary is 'abstr'

Source Code Looks like below for Link and summary.

Link:

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

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

I am using below line of code to capture both (link and summary).

final List<WebElement> links = driver.findElements(By.className("yschttl spt")); 
final List<WebElement> linksSummary 开发者_运维问答= driver.findElements(By.className("abstr"));

But now working at all.

I also tried using below XPaths for Link:

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

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

I cannot use ID as whole because that ID number is not same for all search result links.

Nothing is working. Please help.

thanks in advance.


The main problem is that you are not correctly asking for a classname. The instance of class="yschttl spt" says that the element may be identified by two class names, yschttl or spt. It does not say that the classname is yschttl spt so asking for By.className("yschttl spt") will always fail.

Note that the reason the XPath suggested by @Tarun works is that XPath has no notion of what an HTML class name is or should be. In XPath, @class simply specifies the name of an attribute--with no underlying semantics.

Further, note that a class name may not contain spaces. For more details about specifying class names, see the HTML class attribute specification.


I don't use selenium 2.0 but when I tried //a[@class='yschttl spt'] in Firefox XPath Checker, I got to see all 10 results in page. I am soon to begin with Selenium 2.0, may be I could try then...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜