-1
@Test
public void homework() throws InterruptedException {
    driver.get("http://www.localhost/litecart/admin/");
    driver.findElement(By.cssSelector("td [name = username]")).sendKeys("admin");
    driver.findElement(By.cssSelector("td [name = password]")).sendKeys("admin");
    driver.findElement(By.cssSelector("div.footer [name = login]")).click();
    List<WebElement> elements = driver.findElements(By.cssSelector("ul#box-apps-menu > li"));
    for (WebElement we : elements) {
            we.click();
    }
}

This is my code. elements i want to click I only click the first item and then i get this "stale element reference: element is not attached to the page document". Once you click on a list item, it expands and few more child list-items appear, so idk if that's what's causing the issue.

EDIT: Here's how i did it, i included even child elements. Thanks to the comments below i managed to finally complete this task.

public class HomeWork1 extends TestBase {

@Test
public void homework() throws InterruptedException {
    driver.get("http://www.localhost/litecart/admin/");
    driver.findElement(By.cssSelector("td [name = username]")).sendKeys("admin");
    driver.findElement(By.cssSelector("td [name = password]")).sendKeys("admin");
    driver.findElement(By.cssSelector("div.footer [name = login]")).click();
    List<WebElement> elements = driver.findElements(By.xpath(("//ul//li")));
    for (int i = 1; i <= elements.size(); i++) {
        driver.findElement(By.xpath("//ul[@id='box-apps-menu']/li["+i+"]")).click();
        List<WebElement> element = driver.findElements(By.xpath("//ul[@class='docs']/li"));
        for (int j = 1; j < element.size() + 1; j++){
            driver.findElement(By.xpath("//ul[@class='docs']//li["+j+"]")).click();
        }
        if (i == 17) break;
    }
}

}

1 Answers1

1

or Try this

@Test
public void homework() throws InterruptedException {
    driver.get("http://www.localhost/litecart/admin/");
    driver.findElement(By.cssSelector("td [name = username]")).sendKeys("admin");
    driver.findElement(By.cssSelector("td [name = password]")).sendKeys("admin");
    driver.findElement(By.cssSelector("div.footer [name = login]")).click();
    List<WebElement> elements = driver.findElements(By.xpath("//ul//li"));
    // elements.size() will give you the total number of elements.
    for (i=1;i<=elements.size(),i++) {// This will iterate through all the elements
            driver.findElement(By.xpath("//ul/li["+i+"]")).click(); // clicking on each li element one by one
            //include wait here
    }
}

Yes, you can apply the same logic if you want to work with child items. For example you can use following for first parent item:

List<WebElement> elements = driver.findElements(By.xpath("//ul/li[1]/**")); // ** can be replaced by child identifiers
theGuy
  • 683
  • 5
  • 15
  • 1
    This worked! Would you mind to explain the logic? And also, can i apply the same logic for child list items of the initial list item that was selected? – Vlad Korol Jul 23 '18 at 16:23
  • @user7019709 : this might fail because you are looking for all //li tag. Give the generic xpath for a particular scenario. Btw +1 for logic. – cruisepandey Jul 23 '18 at 16:33
  • Well for this scenario you can easily replace the locator from OP. – cruisepandey Jul 23 '18 at 16:39
  • updated with explanation and yes, you can use the same logic for its child elements. @Cruisepandey, you are right, it will look for all //li tags. I have updated for more generic xpath – theGuy Jul 23 '18 at 16:43
  • for the scenario like this : `//ul[@id='box-apps-menu']/li['"+i+"']` , would just do the job. – cruisepandey Jul 23 '18 at 16:47
  • cause `//ul/li["+i+"]` does not make any sense , because it is very obvious that li tag would be inside the ul. So `//li` and `//ul/li` will return return the same thing. – cruisepandey Jul 23 '18 at 16:50
  • right, as per screenshot provided by Vlad //u/li["+i+"] or //li["+i+"] is best way to identify li tag and they will return same thing. Vlad has to check if there are more ol/ul/dl tags and then change relative xpath for li tag accordingly. – theGuy Jul 23 '18 at 17:54