Selenium2 判断元素是否存在解决方法

本文提供了一个检查网页元素存在的函数,避免了使用Selenium中容易抛出异常的方法。通过尝试使用find_element方法来判断元素是否存在,从而避免了NoSuchElementException异常的困扰。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我目前没有找到相关的API,要么就用selenium1中的对象,但我不想那样做,先出了下面的方法:

WebDriver will throw NoSuchElementException if the object is not
found. I have wrapped that logic in a function to check if the element
exists

    public boolean doesWebElementExist(WebDriver driver, By selector) {
        try {
                driver.findElement(selector);
               return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }
要是谁知道先关的API方法,还请告诉我下,谢谢!

转载于:https://my.oschina.net/willSoft/blog/28120

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值