如何通过Selenium点击超链接?

时间:2016-09-25 09:17:24

标签: java selenium selenium-webdriver bots

我想执行此操作:

1)转到http://www.bbc.co.uk/search?q=(已完成)

2)搜索“apple”(已完成)

3)点击提交(完成)

4)点击第一个结果或所有结果(我在硬编码中执行此操作,如果任何方法可以选择第一个结果,则表示赞赏)(现在正在工作)

我已完成前3个步骤。但是,对于第4步,我不知道该怎么做。因为超链接不包含ID

 <h1 itemprop="headline"><a href="http://www.bbc.co.uk/music/artists/12eec8cf-cd35-410d-91e7-31343029ac39">Apple</a></h1>
                        <p class="summary short">&hellip;The BBC artist page for <em>Apple</em>. Find the best clips, programmes, news and interviews&hellip;</p>
                        <p class="summary medium">&hellip;The BBC artist page for <em>Apple</em>. Find the best clips, programmes, news and interviews.&hellip;</p>

整个代码:

WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Downloads\\geckodriver-v0.10.0-win64\\wires.exe");
driver =new FirefoxDriver();


driver.get("http://www.bbc.co.uk/search?q=");
driver.findElement(By.id("orb-search-q")).sendKeys("apple");//enter apple
driver.findElement(By.id("orb-search-button")).click(); //click submit button

2 个答案:

答案 0 :(得分:-1)

以下任何一项都适合您,但findElements方法应更好地满足您的目的

driver.findElement(By.linkText("Apple")) :Assuming there is only one apple, either ways it will click the first "Apple" link
driver.findElement(By.xpath("//a[contains(@href, "music/artists")

or driver.findElements(By.linkText("Apple")).get(0) 
// or whatever the index of the first link will be
driver.findElements(By.xpath("//a[contains(@href, "music/artists")).get(0)

答案 1 :(得分:-1)

我接近这样的任务,试图找到一般解决方案,以便它可以应用于(希望)任何情况/查询。为此,我们需要查看结果页面的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。有了这些信息,我们可以制作一个类似于ol.search-results > li h1 > a的CSS选择器,它会在标题内找到结果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();

......等等......