gpt4 book ai didi

selenium - 错误后如何继续使用webdriver/selenium

转载 作者:行者123 更新时间:2023-12-03 17:02:44 30 4
gpt4 key购买 nike

我正在用webdriver做一个测试机器人。我有一个场景,它单击一个按钮,打开一个新窗口,并且它通过特定的xpath搜索元素,但是有时没有这样的元素,因为可以将其禁用,并且出现此错误:org.openqa.selenium .NoSuchElementException。我该如何绕过它/继续执行该bot,以便在找不到具有该xpath的元素的情况下仅关闭新窗口并继续执行代码?

最佳答案

在Java中:

List<WebElement> foundElement = driver.findElements(By.xpath("<x-path of your element>"));
if (foundElement.size() > 0)
{
// do whatever you want to do in **presence** of element
} else {
// do whatever you want to do in **absence** of element
}

关于selenium - 错误后如何继续使用webdriver/selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37399391/

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