如何使用selenium webdriver在多个Span标记下提取链接

时间:2013-06-26 12:51:32

标签: javascript selenium-webdriver

我需要计算并显示不同Span标签下可用的链接数。 在下面的HTML代码中,我在Span'Frending Event'下有两个锚链接,在'Recent Free LIVE Webinars'部分下有4个链接。

请提供Xpath以获取这些详细信息。

<!-- language: html -->

<div class="widget widget_text">
<div class="widget-wrap">
<div class="textwidget">
<span style="color: #FF3300;font-size:14px">
<b>Upcoming Event </b>
</span>

    <a href="http://www.itelearn.com/events/mobile-application-testing-   
            career">2nd July 2013:Your Career in Mobile Application Testing </a>

    <a href="http://www.itelearn.com/live-training/qtp-11-live-
            training#">19thJune 2013 : QTP Live Training</a>


    <span style="color: #FF3300;font-size:14px">
             <b>Recent Free LIVE Webinars</b>
             </span>

    <a href="http://www.itelearn.com/events/teachitelenter code hereearn-a-
            mega-webinar">Teach@ITeLearn Initiative </a>

    <a href="http://www.itelearn.com/events/how-to-be-a-software-security-
            testing-expert">How to become a Software Security Testing Expert?</a>

    <a href="http://www.itelearn.com/events/how-to-master-hp-qtp">How to    
            Master QTP?</a>

    <a href="http://www.itelearn.com/events/how-to-master-selenium-video-
            page">How to Master Selenium?     
            </a>

2 个答案:

答案 0 :(得分:1)

对我而言,这将是最简单的方法。找到页面上的所有spans,然后为每个span查找所有anchor元素,打印该列表的长度,然后遍历列表以获取{{1}价值并打印出来。

href

答案 1 :(得分:0)

请在XPATH下方尝试查找元素

//For 'Upcoming Event' span ..
 XPATH = "//span//b[text()='Upcoming Event']//a"
//For 'Recent Free LIVE Webinars' span ...
 XPATH="//span//b[text()='Recent Free LIVE Webinars']//a"