gpt4 book ai didi

selenium - WebDriver 通过 xPath 查找元素,如果未找到元素,则不会超时,屏幕只是卡在那里。

转载 作者:行者123 更新时间:2023-12-02 10:45:57 24 4
gpt4 key购买 nike

我面临一些问题,由于我的测试语句无法找到该元素,测试只是卡在那里(浏览器打开并且无法继续下一个测试)。

我的 TestStatemet 像这样:

driver.findElement(By.xpath("//input[@name='AID' and contains(@value,'sampleDataThatwillNotFound')]"));

测试仅在通过 XPATH 查找时挂起,通过 NAME/ID 查找时没有问题。我已将超时设置为 60 秒,60 秒后,它仍然挂起。

有人以前遇到过这个问题吗?或者有人知道如何解决这个问题吗?

最佳答案

嗯,我遇到了同样的问题,并在webdriver api doc中找到了这个:findElement 不应用于查找不存在的元素,应使用 findElements(By) 并断言零长度响应。

所以我使用类似的东西

List<WebElement> found = driver.findElements(By.id("elementid"));
if (found.size() > 0)
{
// get the 1st element
} else {
// time out
}

解决此问题。在我的例子中,隐式超时与 findElements 配合得很好。

关于selenium - WebDriver 通过 xPath 查找元素,如果未找到元素,则不会超时,屏幕只是卡在那里。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14454866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com