Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch all links and click those links one by one using Selenium WebDriver

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:

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'

and here is my code :

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: firefox on XP (ce0da229-f77b-4fb8-b017-df517845fa78)] -> tag name: a]

as link, I want to eliminate these form result.

like image 478
Shammi Avatar asked Nov 19 '12 05:11

Shammi


People also ask

How do you select all links in Selenium?

New Selenium IDE Testers might be in a situation to find all the links on a website. We can easily do so by finding all elements with the Tag Name "a", as we know that for any link reference in HTML, we need to use "a" (anchor) tag.

How do you get a count of all the links in a webpage and click on it?

The total number of links in a page can be counted with the help of findElements() method. The logic is to return a list of web elements with tagname anchor, then getting the size of that list.

How does Selenium check all links work?

To check broken links in Selenium, the process is simple. On a web page, hyperlinks are implemented using the HTML Anchor (<a>) tag. All the script needs to do is to locate every anchor tag on a web page, get the corresponding URLs, and run through the links to check if any of them are broken.


2 Answers

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

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

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.

It is better to use the following scenario :

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

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

EDIT : Example added

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

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

Hope to be enough.

like image 186
Ioan Avatar answered Nov 15 '22 04:11

Ioan


Credit goes to "loan".

I am also getting "stale exception" so I used 'loan' answer and works perfectly. Just if anyone need to know how to click on each link from results page try this (java)

clickAllHyperLinksByTagName("h3"); where "h3" tag contains hyperlink

 public static void clickAllHyperLinksByTagName(String tagName){
    int numberOfElementsFound = getNumberOfElementsFound(By.tagName(tagName));
    System.out.println(numberOfElementsFound);
    for (int pos = 0; pos < numberOfElementsFound; pos++) {
        getElementWithIndex(By.tagName(tagName), pos).click();
        driver.navigate().back();
    }
}

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

public static WebElement getElementWithIndex(By by, int pos) {
    return driver.findElements(by).get(pos);
}
like image 42
user790049 Avatar answered Nov 15 '22 02:11

user790049