需要了解如何在不使用文本的情况下选择并单击博客标题,因为内容可能会频繁更改。如何在不使用文本的情况下从页面中选择内容?
我没有“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;body=With%20200%20meters%20remaining%20in%20the%20250-kilometer%20&lt;a%20href=&quot;http://www.bicycling.com/news/pro-cycling/ahead-pack-vinokourov-sprints-olympic-gold&quot;&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&p[url]=http://stage7.bicycling.com/blogs/roadscholar/the-surprise-attack-and-when-not-to-do-it&p[title]=The%20Surprise%20Attack%20%28And%20When%20Not%20to%20Do%20It%29&p[summary]=With%20200%20meters%20remaining%20in%20the%20250-kilometer%20&lt;a%20href=&quot;http://www.bicycling.com/news/pro-cycling/ahead-pack-vinokourov-sprints-olympic-gold&quot;&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&url=http://stage7.bicycling.com/blogs/roadscholar/the-surprise-attack-and-when-not-to-do-it&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&description=With%20200%20meters%20remaining%20in%20the%20250-kilometer%20&lt;a%20href=&quot;http://www.bicycling.com/news/pro-cycling/ahead-pack-vinokourov-sprints-olympic-gold&quot;&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;body=At%20some%20point%20this%20week,%20a%20selection%20panel%20consisting%20of%20&lt;a%20href=&quot;http://www.bicycling.com/tags/team-sky&quot;&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&p[url]=http://stage7.bicycling.com/blogs/roadscholar/advanced-team-construction-capstone-seminar&p[title]=Advanced%20Team%20Construction%20%28Capstone%20Seminar%29&p[summary]=At%20some%20point%20this%20week,%20a%20selection%20panel%20consisting%20of%20&lt;a%20href=&quot;http://www.bicycling.com/tags/team-sky&quot;&gt;Team...">
</a></li>
<li class="twitter"><a class="sharebtn" href=
"https://twitter.com/intent/tweet?text=Advanced%20Team%20Construction%20%28Capstone%20Seminar%29&url=http://stage7.bicycling.com/blogs/roadscholar/advanced-team-construction-capstone-seminar&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&description=At%20some%20point%20this%20week,%20a%20selection%20panel%20consisting%20of%20&lt;a%20href=&quot;http://www.bicycling.com/tags/team-sky&quot;&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>
您正试图获得与类的第三个元素'博客-后title'但只有两个这样的元素使用XPath定位器尝试。你可以验证你的初始xpath或HTML吗? – olyv
还有更多元素,但我无法发布更多内容。所以有第三个要素。如果你愿意,可以把这个数字想象成两个。结果是一样的。 – user3493818
好的,这个xpath'// h2 [@ class ='blog-post-title']/a'返回两个href元素。你确实需要其中一个吗? – olyv