如何在不使用文本的情况下从页面中选择内容?

时间:2014-05-21 13:36:31

标签: java html css selenium-webdriver

需要了解如何在不使用文本的情况下选择和点击博客标题,因为内容可能经常更改。

我没有" li"标记所以我之前做的一些事情在这种情况下不起作用。任何帮助都会得到帮助。

这是我尝试过的最后一件事:

driver.findElement(By.xpath("(//h2[contains(@class, 'blog-post-title')])[3]/a")).click();

<!DOCTYPE html>

<html>
<head>
    <title></title>
</head>

<body>
    <div class="view-content">
        <div class="views-row views-row-1">
            <span class="pubdate">July 31, 2012</span>

            <h2 class="blog-post-title"><a href=
            "/blogs/roadscholar/the-surprise-attack-and-when-not-to-do-it">The
            Surprise Attack (And When Not to Do It)</a></h2>

            <p class='field-summary'>With 200 meters remaining in the
            250-kilometer Men’s Olympic Road Race last Saturday, Colombia’s
            Rigoberto Uran made a fatal mistake: He turned his head to see what
            was happening behind him....</p><a class="read-more" href=
            "/blogs/roadscholar/the-surprise-attack-and-when-not-to-do-it">Read
            more</a>

            <ul class="links inline">
                <li class="datetime first"><span>Posted at <time class=
                "timestamp" datetime="2012-07-31T17:41:04-04:00">5:41
                PM</time></span></li>

                <li class="disqus_comments_num"><a data-disqus-identifier=
                "node/28978" href=
                "/blogs/roadscholar/the-surprise-attack-and-when-not-to-do-it#disqus_thread">
                Comments</a></li>

                <li class="email"><a class="sharebtn" href=
                "mailto:?subject=The%20Surprise%20Attack%20%28And%20When%20Not%20to%20Do%20It%29&amp;amp;body=With%20200%20meters%20remaining%20in%20the%20250-kilometer%20&amp;lt;a%20href=&amp;quot;http://www.bicycling.com/news/pro-cycling/ahead-pack-vinokourov-sprints-olympic-gold&amp;quot;&amp;gt;Men%E2%80%99s...%20http://stage7.bicycling.com/blogs/roadscholar/the-surprise-attack-and-when-not-to-do-it">
                </a></li>

                <li class="facebook"><a class="sharebtn" href=
                "http://www.facebook.com/sharer.php?s=100&amp;p[url]=http://stage7.bicycling.com/blogs/roadscholar/the-surprise-attack-and-when-not-to-do-it&amp;p[title]=The%20Surprise%20Attack%20%28And%20When%20Not%20to%20Do%20It%29&amp;p[summary]=With%20200%20meters%20remaining%20in%20the%20250-kilometer%20&amp;lt;a%20href=&amp;quot;http://www.bicycling.com/news/pro-cycling/ahead-pack-vinokourov-sprints-olympic-gold&amp;quot;&amp;gt;Men%E2%80%99s...">
                </a></li>

                <li class="twitter"><a class="sharebtn" href=
                "https://twitter.com/intent/tweet?text=The%20Surprise%20Attack%20%28And%20When%20Not%20to%20Do%20It%29&amp;url=http://stage7.bicycling.com/blogs/roadscholar/the-surprise-attack-and-when-not-to-do-it&amp;via=bicyclingmag">
                </a></li>

                <li class="pinterest"><a class="sharebtn" href=
                "http://pinterest.com/pin/create/button/?url=http://stage7.bicycling.com/blogs/roadscholar/the-surprise-attack-and-when-not-to-do-it&amp;description=With%20200%20meters%20remaining%20in%20the%20250-kilometer%20&amp;lt;a%20href=&amp;quot;http://www.bicycling.com/news/pro-cycling/ahead-pack-vinokourov-sprints-olympic-gold&amp;quot;&amp;gt;Men%E2%80%99s...">
                </a></li>

                <li class="googleplus last"><a class="sharebtn" href=
                "https://plus.google.com/share?url=http://stage7.bicycling.com/blogs/roadscholar/the-surprise-attack-and-when-not-to-do-it">
                </a></li>
            </ul>
        </div>

        <div class="views-row views-row-2">
            <span class="pubdate">June 20, 2012</span>

            <h2 class="blog-post-title"><a href=
            "/blogs/roadscholar/advanced-team-construction-capstone-seminar">Advanced
            Team Construction (Capstone Seminar)</a></h2>

            <p class='field-summary'>At some point this week, a selection panel
            consisting of Team Sky managers and coaches will sit down to make a
            difficult decision: which nine of the team’s 28 riders will be in
            Liege, Belgium, on Saturday, June 30th for the start of the 2012
            Tour de France....</p><a class="read-more" href=
            "/blogs/roadscholar/advanced-team-construction-capstone-seminar">Read
            more</a>

            <ul class="links inline">
                <li class="datetime first"><span>Posted at <time class=
                "timestamp" datetime="2012-06-20T22:01:48-04:00">10:01
                PM</time></span></li>

                <li class="disqus_comments_num"><a data-disqus-identifier=
                "node/28977" href=
                "/blogs/roadscholar/advanced-team-construction-capstone-seminar#disqus_thread">
                Comments</a></li>

                <li class="email"><a class="sharebtn" href=
                "mailto:?subject=Advanced%20Team%20Construction%20%28Capstone%20Seminar%29&amp;amp;body=At%20some%20point%20this%20week,%20a%20selection%20panel%20consisting%20of%20&amp;lt;a%20href=&amp;quot;http://www.bicycling.com/tags/team-sky&amp;quot;&amp;gt;Team...%20http://stage7.bicycling.com/blogs/roadscholar/advanced-team-construction-capstone-seminar">
                </a></li>

                <li class="facebook"><a class="sharebtn" href=
                "http://www.facebook.com/sharer.php?s=100&amp;p[url]=http://stage7.bicycling.com/blogs/roadscholar/advanced-team-construction-capstone-seminar&amp;p[title]=Advanced%20Team%20Construction%20%28Capstone%20Seminar%29&amp;p[summary]=At%20some%20point%20this%20week,%20a%20selection%20panel%20consisting%20of%20&amp;lt;a%20href=&amp;quot;http://www.bicycling.com/tags/team-sky&amp;quot;&amp;gt;Team...">
                </a></li>

                <li class="twitter"><a class="sharebtn" href=
                "https://twitter.com/intent/tweet?text=Advanced%20Team%20Construction%20%28Capstone%20Seminar%29&amp;url=http://stage7.bicycling.com/blogs/roadscholar/advanced-team-construction-capstone-seminar&amp;via=bicyclingmag">
                </a></li>

                <li class="pinterest"><a class="sharebtn" href=
                "http://pinterest.com/pin/create/button/?url=http://stage7.bicycling.com/blogs/roadscholar/advanced-team-construction-capstone-seminar&amp;description=At%20some%20point%20this%20week,%20a%20selection%20panel%20consisting%20of%20&amp;lt;a%20href=&amp;quot;http://www.bicycling.com/tags/team-sky&amp;quot;&amp;gt;Team...">
                </a></li>

                <li class="googleplus last"><a class="sharebtn" href=
                "https://plus.google.com/share?url=http://stage7.bicycling.com/blogs/roadscholar/advanced-team-construction-capstone-seminar">
                </a></li>
            </ul>
        </div>
    </div>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

Jquery可以使用标准的css样式选择器,例如class和id。 E.g。

// Class
$('.my-class')
// Id
$('#my-id')
// Element
$('div')

点击......

// You don't have to pass event, but it may be useful
$('.my-class').click(function (event) {
    /* Do something */
});

希望这会有所帮助。还有一些快速谷歌搜索应该向您展示。 :)

答案 1 :(得分:0)

尝试使用下一个查询(//h2[@class='blog-post-title'])[position()=1],其中position()指定需要抓取的元素数量。

答案 2 :(得分:0)

通过指定索引

尝试使用xpath定位器
By.xpath("(//h2[@class='blog-post-title']/a)[3]")