即使元素显示在Selenium中,每次也会执行catch语句 [英] catch statement is executed every time even if element is displayed in Selenium

查看:87
本文介绍了即使元素显示在Selenium中,每次也会执行catch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Receiving no element exception when for below code.

如果要显示adminsearchuserid并且显示元素emailtextbox,然后要输入登录块",我想打印进入管理块"

I want to Print "Entered the admin block" if adminsearchuserid is displayed AND if element emailtextbox displayed then "Entered the LOGIN block"

但是即使显示emailtextbox,我的代码仍显示catch语句.我不明白我在哪里做错了

but my code is showing catch statement even if emailtextbox is displayed. I dont understand where i am doing mistake

Case "User ID":
        try {
            System.out.println("Entered the try block");

            if (adminSearchPo.adminSearchUserId.isDisplayed()) {
                System.out.println("Entered the admin block");

            } else if (lpo.emailTextBox.isDisplayed()) {
                System.out.println("Entered the LOGIN block");
            }

        } catch (org.openqa.selenium.NoSuchElementException e) {
            System.out.println("Entered the exception block");
        }    
break;

推荐答案

您需要使用try/catch处理每个if条件.

You need to handle each if condition with try/catch.

让我们说,正如您所说的,显示了电子邮件文本框,代码正在逐行执行并到达

Let say, as you said emailtextbox is displayed, code is executing line by line and came to

    if (adminSearchPo.adminSearchUserId.isDisplayed()) {

这里的元素如果没有显示条件会导致异常,那么它将被捕获.因此,如果尝试两个条件,则没有办法走另一条路.

here element in if condition is not displayed,leads to exception, then it will go to catch. So there is no way to go another if as two if conditions are in try.

这篇关于即使元素显示在Selenium中,每次也会执行catch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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