-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();
    }
}

这是我的代码。我想单击的元素 我只单击第一项,然后我得到这个“陈旧的元素引用:元素未附加到页面文档”。单击列表项后,它会展开并出现更多的子列表项,所以如果这是导致问题的原因,请注意。

编辑:我是这样做的,我什至包括了子元素。感谢下面的评论,我终于完成了这项任务。

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

}

4

1 回答 1

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.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
于 2018-07-23T16:17:55.493 回答