@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.
这是我的代码。我想点击的元素我只点击第一个项目然后我得到这个“陈旧的元素引用:元素没有附加到页面文档”。一旦你点击一个列表项,它就会扩展,并且会出现更多的子列表项,所以如果这是导致问题的原因,那就是idk。
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 个解决方案
#1
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
#1
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