0

I want to switch to an iframe which contains some links in it. I need to switch to that iframe and click the links one by one. Here is my code,

public static void main(String[] args) {
    // TODO Auto-generated method stub
    WebDriver driver = new FirefoxDriver();
    driver.get("http://timesofindia.indiatimes.com/home");
    WebDriverWait wait = new WebDriverWait(driver,200);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.id("riShop")));
    driver.switchTo().frame("riShop");

    List<WebElement> lst = driver.findElements(By.tagName("a"));
    for(int i = 0; i < lst.size(); i++) {
        lst.get(i).click();
        driver.navigate().back();
    }
}

In the above code only the first link gets clicked and then I get an exception like "unable to locate the next element" NoSuchException

How do I fix this?

Kev
  • 118,037
  • 53
  • 300
  • 385
Kiran
  • 11
  • 1
  • I would say that when you click the link you just miss all the content of the page, so it is no more possible to use the other links. You should open the link in another page. – 5agado Aug 22 '14 at 10:29
  • @5agado Could you please...clarify your point a bit more....so that I could specifically use that itself...Basically i just want to click the all the links that is present inside the frame that's it – Kiran Aug 22 '14 at 10:43
  • Try to use the information in this answer : http://stackoverflow.com/a/24514207/3805954 There is a code to open each link in a new window, this way you can close the new window when you want, and open the next link just after – singe3 Aug 22 '14 at 11:55

2 Answers2

1

Is your Exception is NoSuchElement or StaleElementException error?

I am hoping the error is StaleElementException. Reason being, when you navigate away from the page and once you come back. previous objects will become "Stale".

Following is the logic which i got from SO when i faced this problem earlier:

for (int i=0; true; i++)
{
    List<WebElement> links = driver.findElements(By.tagName("a"));
    if (i >= links.size())
        break;
    links.get(i).click();
    System.out.println(driver.getTitle());
    driver.navigate().back();
}

Let us know if the above helps.

Uday
  • 1,433
  • 10
  • 36
  • 57
  • @Kiran, can u let me know what didnt work for you? Can you also post the code which u r trying? – Uday Aug 25 '14 at 14:39
  • @Uday I have already pasted the whole code above.,though when I enter into the frame and try clicking all the links one by one..the links open in the other window.but that should not matter anyhow as I just have to click the links in the main window one be one..I can give you the URL:-http://timesofindia.indiatimes.com/home ,in this page please find the frame with ID-"riShop",in this fram I have to click the links one by one.please suggest me – Kiran Aug 27 '14 at 13:59
  • @Uday.Output:-Iam able to click the first link and then I get an exception like---->Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted.. – Kiran Aug 27 '14 at 14:18
  • Did you tried my code, the below code worked perfect in my system. – Uday Aug 27 '14 at 16:43
1

Following is the code which i tried for Google site.

Put extra validation as link.getText() as many links with empty texts may exists and link.click may not work there. So just make the "if" condition before clicking on it as specified in below code

public static void main(String[] args) 
{
        // TODO Auto-generated method stub
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.google.com");

    for (int i=0; true; i++)
    {
        List<WebElement> links = driver.findElements(By.tagName("a"));
        if (i >= links.size())
            break;
        if(!links.get(i).getText().isEmpty())
        {
            links.get(i).click();
            System.out.println(driver.getTitle());
            driver.navigate().back();
        }
    }

}

The logic is simple each iteration in the for loop re-identifies the object but we are navigating to next link by increasing the index value.

Uday
  • 1,433
  • 10
  • 36
  • 57