如何使用Selenium WebDriver逐个获取所有链接并单击这些链接 [英] How to fetch all links and click those links one by one using Selenium WebDriver

查看:1330
本文介绍了如何使用Selenium WebDriver逐个获取所有链接并单击这些链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium WebDriver和java。

I am using Selenium WebDriver with java.

我从网页获取所有链接并尝试逐个点击每个链接。我收到以下错误:

I am fetching all links from webpage and trying to click each link one by one. I am getting below error:


错误org.openqa.selenium.StaleElementReferenceException:在缓存中找不到元素 - 可能页面已更改,因为它被查询
命令持续时间或超时:30.01秒
有关此错误的文档,请访问: http://seleniumhq.org/exceptions/stale_element_reference.html
构建信息:版本:'2.25.0',修订版:'17482',时间:'2012-07-18 21:09:54'

error org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up Command duration or timeout: 30.01 seconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:09:54'

这是我的代码:

public void getLinks()throws Exception{
    try {
        List<WebElement> links = driver.findElements(By.tagName("a"));
        int linkcount = links.size(); 
         System.out.println(links.size()); 
          for (WebElement myElement : links){
         String link = myElement.getText(); 
         System.out.println(link);
         System.out.println(myElement);   
        if (link !=""){
             myElement.click();
             Thread.sleep(2000);
             System.out.println("third");
            }
            //Thread.sleep(5000);
          } 
        }catch (Exception e){
            System.out.println("error "+e);
        }
    }

实际上,它显示在输出中

actually, it's displaying in output


[[FirefoxDriver:XP上的firefox(ce0da229-f77b-4fb8-b017-df517845fa78)] - >标签名称:a]

[[FirefoxDriver: firefox on XP (ce0da229-f77b-4fb8-b017-df517845fa78)] -> tag name: a]

作为链接,我想消除这些表格结果。

as link, I want to eliminate these form result.

推荐答案

没有一个好主意有以下场景:

There is no such a good idea to have following scenario :

for (WebElement element : webDriver.findElements(locator.getBy())){
  element.click();
}

为什么?因为无法保证 element.click(); 对其他找到的元素没有影响,所以 DOM 可能会被更改,因此 StaleElementReferenceException

Why? Because there is no guarantee that the element.click(); will have no effect on other found elements, so the DOM may be changed, so hence the StaleElementReferenceException.

最好使用以下方案:

int numberOfElementsFound = getNumberOfElementsFound(locator);
for (int pos = 0; pos < numberOfElementsFound; pos++) {
  getElementWithIndex(locator, pos).click();
}

这是更好的,因为你总是会拿 WebElement 刷新,即使之前的点击对它有一些影响。

This is better because you will always take the WebElement refreshed, even the previous click had some effects on it.

编辑:已添加示例

  public int getNumberOfElementsFound(By by) {
    return webDriver.findElements(by).size();
  }

  public WebElement getElementWithIndex(By by, int pos) {
    return webDriver.findElements(by).get(pos);
  }

希望足够。

这篇关于如何使用Selenium WebDriver逐个获取所有链接并单击这些链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆