错误后如何继续 webdriver/selenium [英] How to continue webdriver/selenium after error

查看:35
本文介绍了错误后如何继续 webdriver/selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm doing a test bot with webdriver. I have a scenario where it clicks on a button, a new windows open, and it searches for an element by a specific xpath, but sometimes there is no such element because it can be disabled and i get this error: org.openqa.selenium.NoSuchElementException. How can i bypass it/continue the bot so it just closes the new windows if it doesn't find the element with that xpath and just continue with the code?

推荐答案

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

这篇关于错误后如何继续 webdriver/selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆