0

I am trying to use the link https://www.bloomingdales.com/ Click child links of each menu .Below is the code i tried .

public void iClickOnFOBSShouldVerifyTheRespectivePages() throws Throwable {

    List allElements = Elements.findElements(By.xpath("//ul[@id='mainNav']/li/a"));

    for (int i = 0; i <= allElements.size(); i++) {
        List<WebElement> links = Elements.findElements(By.xpath("//ul[@id='mainNav']/li/a"));
        WebElement ele = links.get(i);
        ele.click();


        List<WebElement> childlinks = Elements.findElements("left_facet.left_nav");
        for (int j = 0; j <= childlinks.size(); j++) {
            List<WebElement> ele2 = Elements.findElements(By.xpath("left_facet.left_nav"));
            WebElement ele3 = links.get(i);
            ele3.click();
            Navigate.browserBack();
        }
    }

Below is the error i am getting

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

2 Answers2

0

Try This::List<WebElement>ele = driver.findElements(By.xpath("//ul[@id='mainNav']/li/a")); Actions act =new Actions(driver);

    Thread.sleep(4000);

    for (int i = 0; i <= ele.size(); i++) {
        WebElement a =ele.get(i);
        act.moveToElement(a).build().perform();
        Thread.sleep(2000);
        List<WebElement>ChildMenu=driver.findElements(By.xpath("//nav[@id='nav']/div[2]/div/div/div[@class='flyoutCol']/div/ul/li"));
        System.out.println("Sub-Menu="+ChildMenu.size());
        for(int j=0;j<ChildMenu.size();j++){
            ChildMenu.get(i).click();
             Thread.sleep(2000);
             driver.navigate().back();
             Thread.sleep(2000);

            act.moveToElement(a).build().perform();
0

On clicking any of the links, the browser will open a new page. At that moment the elements in the page corresponding to allElements, ele and ele2 disappear ("element is not attached to the page document") in the browser and are thus not valid anymore. I would expect that the first click on a submenu child would work but anything after that will fail.

What you could do is first check how many children each submenu has, store this in an array and then create a double loop somewhat similar to what you have already done. For each submenu and submenu child click, you can't use any of the webelements in memory of your program because their state was changed so you would need to initialize a completely new webelement.

With xpath you should be able to reference directly to an element like 'parent - 3rd submenu - 7th submenu child'. I am unfortunately not able to quickly create working code, otherwise I would have done so.

What I am wondering is: what is the purpose of this code? You don't do any assertions, so if any submenu child click leads to an error page, your code will not fail. Is this desired? It might not even fail if clicking doesn't even open an error page. I guess this code would fail if some of the submenu children are present in the HTML but for some reason not visible.

If this is meant to be a test, I would recommend to make a handful of separate tests that cover a few examples (and assert that clicking leads to the expected page) instead of looping through everything. This will make the tests far more understandable for yourself and others.

Pieter A
  • 166
  • 3