0

I have create below script using List Element and For loop Conditions. Please review my scenario

Scenario:

  1. Open Home Page > Click on drop down > Open the Drop down List.

  2. Click on First option > Redirect to the specific screen and Verify the title.

  3. Go back the same screen and Same scenario will be follow 4 times.

When I have execute my below script in Eclipse I have found the error. In my below coding first click is working but second click is not working in my drop-down.

@Test
public void  toVerifyHelpLink() throws IOException, InterruptedException{
        File src = new File("Object_Repo.properties");
        FileInputStream fis = new FileInputStream(src);
        pro = new Properties();
        pro.load(fis);

        Thread.sleep(2000);
         WebElement linksdiv = driver.findElement(By.xpath(pro.getProperty("new_ul")));
         List<WebElement> Links = linksdiv.findElements(By.tagName("a"));
         System.out.println(Links.size());  
         for(int i=0; i<= Links.size()-1; i++)
     {
       driver.findElement(By.xpath(pro.getProperty("customerservice"))).click();
       String subCategory = Links.get(i).getText();     
       System.out.println(subCategory);
       Links.get(i).click();
            if(!subCategory.equalsIgnoreCase("404")){
                System.out.println("Title displaying as exepected");
            }
        driver.navigate().back();
        linksdiv = driver.findElement(By.xpath(pro.getProperty("new_ul")));
        Thread.sleep(2000);
    }
}

This is the HTML structure is as below

<div class="customer-service effect">
<a href="">customer service</a>
<ul>
<li><a href="">Help</a></li>
<li><a href="">FAQs</a></li>
<li><a href="">Contact Us</a></li>
<li><a href="">Shipping and Returns</a></li>
</ul>
</div>
Fenil Patel
  • 1,528
  • 11
  • 28
  • 2
    Possible duplicate of [Selenium Webdriver - Stale element exception when clicking on multiple dropdowns. DOM dint change.](https://stackoverflow.com/questions/45056398/selenium-webdriver-stale-element-exception-when-clicking-on-multiple-dropdowns) – undetected Selenium Jan 13 '18 at 10:17

2 Answers2

0

try add one more line in the for loop:

driver.navigate().back();
linksdiv = driver.findElement(By.xpath(pro.getProperty("new_ul")));
Links = linksdiv.findElements(By.tagName("a")); // add this line to find all "a" again
yong
  • 13,357
  • 1
  • 16
  • 27
0

Stale element exception - tells you that your page was changed, but instance of your web element was not. It means when the HTML code of your page is changing you should find that element again, because html changed. That is how selenium works.

Also I see typical mistakes in your code, please take a look at: http://selenide.org/

And do not use thread sleep: https://saucelabs.com/blog/how-to-avoid-threadsleep-in-test-automation

nick318
  • 575
  • 4
  • 18