By clicking on the link one by one from the same page using selenium webdriver

I am trying to click on the status link specified in the url, then print the title of the next page and go back, and then click on the other status link dynamically using for the loop. But the loop stops after the initial value.
My code is as follows:

 public class Allstatetitleclick {  
    public static void main(String[] args) {        
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://www.adspapa.com/");      

        WebElement catBox = driver.findElement(By.xpath("html/body/table[1]/tbody/tr/td/table/tbody/tr/td[1]/table"));
        List<WebElement> catValues = catBox.findElements(By.tagName("a"));

        System.out.println(catValues.size());

        for(int i=0;i<catValues.size();i++){

            //System.out.println(catValues.get(i).getText());       
            catValues.get(i).click();
            System.out.println(driver.getTitle());
            driver.navigate().back();

        }
        System.out.println("TEST");
    }
}
+3
source share
2 answers

The problem is that after navigating back, the items found will expire. Therefore, we need to update the code to update the elements after going back.

Update the code below:

        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://www.adspapa.com/");      

        WebElement catBox = driver.findElement(By.xpath("html/body/table[1]/tbody/tr/td/table/tbody/tr/td[1]/table"));
        List<WebElement> catValues = catBox.findElements(By.tagName("a"));

        for(int i=0;i<catValues.size();i++)
        { 
            catValues.get(i).click();
            System.out.println(driver.getTitle());
            driver.navigate().back();
            catBox = driver.findElement(By.xpath("html/body/table[1]/tbody/tr/td/table/tbody/tr/td[1]/table"));
            catValues = catBox.findElements(By.tagName("a"));
        }
        driver.quit();

I tested the above code at my end and its working tone. You can improve the code above according to your use.

+3

. . . , . , . . , .

-2

All Articles