@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();
}
}
这是我的密码。我要单击的元素我只点击了第一个项目,然后我得到了这个"过时的元素引用:元素没有附加到页面文档"。一旦你点击了一个列表项,它就会展开,只会出现几个子列表项,所以如果这是问题的原因,请确认。
编辑:以下是我的做法,我甚至包括了儿童元素。多亏了下面的评论,我终于完成了这项任务。
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;
}
}
}
或尝试此
@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
}
}
是的,如果要处理子项,可以应用相同的逻辑。例如,您可以将以下内容用于第一个父项:
List<WebElement> elements = driver.findElements(By.xpath("//ul/li[1]/**")); // ** can be replaced by child identifiers