2014-05-21 43 views
0

需要了解如何在不使用文本的情况下选择并单击博客标题,因为内容可能会频繁更改。如何在不使用文本的情况下从页面中选择内容?

我没有“li”标签,所以以前做过的一些事情在这种情况下不起作用。任何帮助将apprecaited。

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

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> 
+0

您正试图获得与类的第三个元素'博客-后title'但只有两个这样的元素使用XPath定位器尝试。你可以验证你的初始xpath或HTML吗? – olyv

+0

还有更多元素,但我无法发布更多内容。所以有第三个要素。如果你愿意,可以把这个数字想象成两个。结果是一样的。 – user3493818

+0

好的,这个xpath'// h2 [@ class ='blog-post-title']/a'返回两个href元素。你确实需要其中一个吗? – olyv

回答

0

jQuery的可以使用标准的CSS样式选择,如class和id。例如。

// 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 */ 
}); 

希望这有助于。还有一些Google搜索快速显示给你。 :)

+0

谢谢,但我没有使用jquery我正在使用Java。 :( – user3493818

+0

大声笑,我确实对Java和JQuery标签有点困惑,抱歉我不能帮助,希望你很快得到解决方案。 – JakeSidSmith

0

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

0

通过指定索引

By.xpath("(//h2[@class='blog-post-title']/a)[3]") 
相关问题