问答 求助为何会报 no such element: Unable to locate element: {"method":"css selector","selector":"[testid="editItem"]"} 错误

jeneral2019 · 2021年03月03日 · 最后由 jeneral2019 回复于 2021年03月03日 · 2694 次阅读

代码:

public WebElement findElement(By by){
    if (!waitEle(by)){
        throw new RuntimeException("cannot find ele : " + by);
    }
    return webElement.findElement(by);
}
private Boolean waitEle(By by) {
    if (by == null){
        return false;
    }
    WebDriverWait wait = new WebDriverWait(driver, waitTimeDefault);
    try {
        wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(by));
    } catch (Exception e) {
        return false;
    }
    return true;
}

报错如下:

我先等待元素存在再去查找元素,为何还是找不到该元素

共收到 1 条回复 时间 点赞
1楼 已删除

原因找到了,是由于 wait 是在 driver 下找的,在 driver 下含有这个元素,但是最后 webElement.findElement(by) 的 webElement 没有该元素。

jeneral2019 关闭了讨论 03月04日 10:18
需要 登录 後方可回應,如果你還沒有帳號按這裡 注册