0

I just cracked my head trying to find how make it work. I am trying to force Selenium open link by link, but it opens on first link again and again, console output shows that loop is working correctly. Tried to use while loop but it doesn`t work too. I am trying to open link after link and change number of the li element to open further link.

for (int footer_links = 1; footer_links < 6; footer_links++) { WebElement self_service_bi = driver.findElement(By.xpath("//div/div/ul/li['$footer_links']/a")); self_service_bi.click(); File srcFile1 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); File targetFile1 = new File("D:\\DataPineScreenshots\\" + driver.getTitle() + ".png"); FileUtils.copyFile(srcFile1, targetFile1); driver.navigate().back(); System.out.print(footer_links + "\n"); } 
1
  • What is the exception? StaleElementReferenceExxception?
    – Saifur
    CommentedJan 18, 2015 at 16:09

2 Answers 2

1

fix your syntax

By.xpath("//div/div/ul/li['$footer_links']/a") 

by

By.xpath("//div/div/ul/li[" + footer_links + "]/a") 
1
  • I think it does provide an answer to the question
    – vins
    CommentedJan 18, 2015 at 23:57
0

driver.findElement will always return the first element of the type. Use driver.findElements function to get list of all matching the given xpath. But dont do that in loop cause everytime it will open the same link. Try out like:

List<String> lstUrls = new ArrayList<String>(); List<WebElement> lstEle = driver.findElements(By.xpath("//div/div/ul/li['$footer_links']/a")); for (WebElement element : lstEle) lstUrls.add(element.getAttribute("href")); for (String string : lstUrls) { driver.get(string) File srcFile1 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); File targetFile1 = new File("D:\\DataPineScreenshots\\" + driver.getTitle() + ".png"); FileUtils.copyFile(srcFile1, targetFile1); driver.navigate().back(); System.out.print(footer_links + "\n"); } 
0

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.