【问题标题】:How to click the hyperlink via Selenium?如何通过 Selenium 单击超链接?
【发布时间】:2017-02-02 18:12:42
【问题描述】:

我想这样做:

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

2) 搜索“苹果”(完成)

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

【问题讨论】:

    标签: java selenium selenium-webdriver bots


    【解决方案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)
    

    【讨论】:

    • 在一般情况下,如果没有一个链接完全是搜索词怎么办,例如“苹果”?这行不通。
    • 然后他们可以使用 By.partialLinkText 方法。这个概念仍然保持不变。
    • 如果使用参数化搜索字符串作为文本会更好
    • 我的意思是 OP 要求点击第一个链接。您的答案可能适用于此示例,但如果您更改搜索文本,则不会。
    • 当然,答案有针对该问题的替代解决方案。 linkText 解决方案适用于需要单击带有搜索字符串的第一个链接的情况。
    【解决方案2】:

    我处理此类任务的目的是试图找到一个通用解决方案,以便将其应用于(希望)任何情况/查询。为此,我们需要查看结果页面的 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 &gt; li h1 &gt; 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();
    

    ...等等...

    【讨论】:

    • 我正在测试代码。这里有一个问题。即使对于我的代码`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 3 步骤在这里。有时,代码会在步骤 1 或步骤 2 中停止。我想应该添加暂停。然后我添加了这个driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 但这不起作用。为什么
    • 这些步骤不需要暂停。为什么停止了?有错误还是?我会避免使用隐式等待。需要时使用显式等待 (WebDriverWait)。
    • 因为有时它会在访问 BBC 网站的阶段停止。有时它会停止在输入关键字“apple”。有时它可以执行其余任务。我不知道问题是什么。
    • 听起来你在后台有一些 ajax 或 javascript 的时间问题。您应该构建一个服务员进行轮询,直到可以单击搜索按钮。 seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/…
    猜你喜欢
    • 1970-01-01
    • 2012-08-28
    • 2021-08-07
    • 2016-12-28
    • 2019-04-16
    • 2013-05-19
    • 2013-10-04
    • 2015-03-01
    • 1970-01-01
    相关资源
    最近更新 更多