【问题标题】:Boolean expression in If condition is not working in seleniumIf 条件中的布尔表达式在 selenium 中不起作用
【发布时间】:2021-07-17 03:11:33
【问题描述】:

我正在尝试创建一个自动化场景,在该场景中输入登录凭据并单击登录按钮,然后搜索元素。如果显示元素,则打印有效凭据,如果未显示元素,则执行其他操作,但单击登录按钮后代码不起作用。 这里我为它创建了一个方法。

public void toCheckLoginVerification() throws InterruptedException,MalformedURLException {
    clickOnSignInbutton();
    enterEmailId("s243535@yopmail.com");
    enterPassword("Test@123");
    clickOnSignInButtonOnSignInScreen(); //code not working after this.
    boolean x = driver.findElement(By.xpath("//android.view.View[@content-desc='Trips']")).isDisplayed();
    if(x==true) {
        System.out.println("Valid Credentials");
    } else {
        System.out.println("Invalid Credentials");
        clearEmailid();
        clearPassword();
        enterEmailId("s2@yopmail.com");
        enterPassword("Test@123");
        clickOnSignInButtonOnSignInScreen();
        clickOnProfileButtonOnHomeScreen();
    }
}

我在其他类中创建了上述方法,但在这里调用它们。 更具体地说,我使用的是 Appium。

【问题讨论】:

  • “它不起作用”不是问题描述。它应该做什么,基于保证的输入,你看到它做什么,以及你已经做了什么来调试问题(例如,查找并验证你正在做的 API 调用,放置断点来检查值等)
  • @Mike'Pomax'Kamermans 抱歉,如果我无法描述我的问题,但这就是我目前所知道的全部。
  • 大概是您编写了这段代码,所以:您是否查看了您正在调用的函数的 Selenium API 文档?他们是否说您编写的代码是使用它们的正确方法?
  • @Mike'Pomax'Kamermans 是的,我已经检查过了。
  • 那么请记住,这不是一个通用的帮助论坛,有posting guidelines,您应该已经阅读了,所以请更新您的帖子以关注他们:将详细信息放在您的帖子中。这包括说出您已经检查/验证/调试/等的内容。这样一来,您寻求帮助的人就不会只是建议您做您已经做过的事情,而不会告诉他们您已经做过。

标签: java selenium selenium-webdriver appium


【解决方案1】:

您正在检查元素是否立即显示.. 相反,您应该使用 selenium 的隐式或显式等待机制

【讨论】:

  • 我正在使用隐式等待。所以,我认为等待在这里不是问题,而且我一开始就使用了无效的凭据。所以,它应该直接进入else语句。
【解决方案2】:

您应该使用显式等待而不是隐式等待。
使用您的代码 selenium 在它刚刚创建但仍未完全加载/渲染的那一刻找到元素。因此,当您的 driver.findElement(By.xpath("//android.view.View[@content-desc='Trips']")) 返回元素时,它仍然不显示/不可见。
虽然您应该等到发现元素可见。
因此,您可以使用以下方法:

public boolean waitForElementToBeVisible(String xpath, int delay) {
        wait = new WebDriverWait(driver, delay);
        try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(element));
            return true;
        }catch (Exception e){
            return false;
        }
    }

现在你可以说:

if(waitForElementToBeVisible("//android.view.View[@content-desc='Trips']",10)){
    System.out.println("Valid Credentials");
} else{
    System.out.println("Invalid Credentials");
    clearEmailid();
    clearPassword();
    enterEmailId("s2@yopmail.com");
    enterPassword("Test@123");
    clickOnSignInButtonOnSignInScreen();
    clickOnProfileButtonOnHomeScreen();
}

【讨论】:

    【解决方案3】:

    如果布尔表达式(条件)为真,则将执行 if 块中的“语句”。如果布尔表达式为假,则不会执行 if 块中的语句,而是执行 if 块之后的其余代码。

    READ THIS

    if(waitForElementToBeVisible("//android.view.View[@content-desc='Trips']",10)){
        System.out.println("Valid Credentials");
    } else{
        System.out.println("Invalid Credentials");
        clearEmailid();
        clearPassword();
        enterEmailId("s2@yopmail.com");
        enterPassword("Test@123");
        clickOnSignInButtonOnSignInScreen();
        clickOnProfileButtonOnHomeScreen();
    }
    
    public boolean waitForElementToBeVisible(String xpath, int delay) {
            wait = new WebDriverWait(driver, delay);
            try {
                wait.until(ExpectedConditions.visibilityOfElementLocated(element));
                return true;
            }catch (Throwable t){
                return false;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2017-02-05
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      相关资源
      最近更新 更多