我处理此类任务的目的是试图找到一个通用解决方案,以便将其应用于(希望)任何情况/查询。为此,我们需要查看结果页面的 HTML 并找到搜索结果的容器,以便我们可以缩小单个搜索结果的范围并找到每个结果的链接。然后我们可以根据一些标准选择我们想要的任何结果,然后点击该链接等。
如果查看结果页面的HTML,搜索结果都包含在这个元素中。
<ol class="search-results results" start="1" data-total-results="16514">
由像这样的孩子组成
<li data-result-number="1">
<li data-result-number="2">
<li data-result-number="3">
<li data-result-number="4">
例如,这里是整个第一个结果
<li data-result-number="1">
<article class=" media-text" itemscope="">
<aside class="flags top"></aside>
<aside class="flags mid"></aside>
<div>
<h1 itemprop="headline">
<a href="http://www.bbc.co.uk/music/artists/12eec8cf-cd35-410d-91e7-31343029ac39">Apple</a>
</h1>
<p class="summary short">…The BBC artist page for <em>Apple</em>. Find the best clips, programmes, news and interviews…</p>
<p class="summary medium">…The BBC artist page for <em>Apple</em>. Find the best clips, programmes, news and interviews.…</p>
<p class="summary long">…The BBC artist page for <em>Apple</em>. Find the best clips, programmes, news and interviews.…</p>
<footer>
<dl>
<dt>Tags</dt>
<dd><span class="signpost-site" data-site="music">Music</span><span class="signpost-section">Artists</span>
</dd>
</dl>
</footer>
</div>
<a href="http://www.bbc.co.uk/music/artists/12eec8cf-cd35-410d-91e7-31343029ac39" class="rs_touch"></a>
<span class="divide"></span>
</article>
</li>
由此我们可以看到标题 Apple 位于 h1 标记中,并且包含我们要查找的 A。有了这些信息,我们可以制作一个 CSS 选择器,例如 ol.search-results > li h1 > a,它将在标题内找到结果 A 标记。现在我们有了这个,我们可以编写如下代码来转储每个搜索结果的标题。
String searchTerm = "apple";
driver.get("http://www.bbc.co.uk/search?q=" + searchTerm);
List<WebElement> headingLinks = driver.findElements(By.cssSelector("ol.search-results > li h1 > a"));
for (WebElement headingLink : headingLinks)
{
System.out.println(headingLink.getText());
}
转储以下内容
Apple
Apple of my Eye
Down on the Farm: Series 1: Farm Park and Apple Crisps
Cinnamon, apple and custard Danish
Apple, sultana and cinnamon swirls
The logic in Apple buying McLaren
Supercar maker McLaren denies Apple investment report
Scotch egg with apple
Cider and apple cake
Simon Mayo Drivetime: Blackberry and Apple Bake
此时,如果要点击第一个链接,只需将代码改为
String searchTerm = "apple";
driver.get("http://www.bbc.co.uk/search?q=" + searchTerm);
List<WebElement> headingLinks = driver.findElements(By.cssSelector("ol.search-results > li h1 > a"));
headingLinks.get(0).click();
第二个链接是
headingLinks.get(1).click();
...等等...