Reputation: 31
I'm trying to click on state link given url then print title of next page and go back, then click another state link dynamically using for loop. But loop stops after initial value.
My code is as below:
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");
}
}
Upvotes: 3
Views: 5262
Reputation: 1
Note: I have executed this and its working fine now.It will give you the size and clicking on every link.It was a issue where you need to go inside the Frame. It has a Frame where you need to switch. Please copy and paste and execute it. Hope you find your answer.
Upvotes: -2
Reputation: 648
The problem is after navigating back the elements found previously will be expired. Hence we need to update the code to refind the elements after navigate 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 have tested above code at my end and its working fine.You can improve the above code as per your use.
Upvotes: 3