@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;
}
}
}
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