如果下一个元素不存在则中断 for 循环

Break a for loop if the next element does not exist

/**
 * This method clicks an element if it exist
 * @param by
 * @throws InterruptedException
 */
public void optionalClick(By by) throws InterruptedException
{
    log.divider("Check if Element Exist or Not......");     
    //String continueFB_Page = "";
    //continueFB_Page = driver.getPageSource();
    Thread.sleep(4000);     
    if(driver.findElement(by).isDisplayed())         
    {
        log.log("Element IS present... Clicked");
        log.log("Indo App IS NOT installed on FB acct.......Signing in for the first time..");
        driver.findElement(by).click();
        Thread.sleep(3000);         
    }

}

/**
 * This method Signs user in using FB social signIn 
 * Takes the Constant signIn variable as the Link
 * 
 * @param link
 * @throws InterruptedException
 */
public void signInFacebook(String link) throws InterruptedException {
    log.header("USER SIGN IN VIA FACEBOOK");
    log.step("Click the Sign In Link");
    clickLink(link);
    Thread.sleep(3000);
    log.log("click success.......");
    log.step("Click 'Sign in with Facebook' Link ");


    // Store the current window handle
    String parentHandle  = driver.getWindowHandle();
    //boolean presentPage = driver.equals(parentHandle);


    // Perform the click operation that opens new window
    driver.findElement(By.cssSelector(CONSTANTs.FB_SIGN_IN)).click();
    Thread.sleep(5000);     

    // Switch to new window opened
    log.divider("Facebook Login Screen.....Enter Details Below");
    for (String childHandle : driver.getWindowHandles()) {
        driver.switchTo().window(childHandle);          

        if (!childHandle.equals(parentHandle)) {                
            log.step("Enter Email address");
            driver.findElement(By.cssSelector(CONSTANTs.FB_EMAIL_TXTBOX))
                    .sendKeys(CONSTANTs.EMAIL_ADDRESS);
            Thread.sleep(2000);
            log.step("Enter password");
            driver.findElement(By.cssSelector(CONSTANTs.FB_PASSWORD_TXTBOX))
                    .sendKeys(CONSTANTs.FB_PASSWORD);
            Thread.sleep(2000);
            log.step("Click Log_In button");
            driver.findElement(By.cssSelector(CONSTANTs.LOG_IN)).click();
            Thread.sleep(5000); 

            log.log("am here ... 1");
            WebDriver switchPage = driver.switchTo().window(parentHandle);
            log.log("am here ... 2");
            if(switchPage.getWindowHandle().contains(parentHandle))
            {
                driver.switchTo().window(childHandle);
                optionalClick(By.cssSelector(CONSTANTs.OK_CONTINUE_AS));
                driver.switchTo().window(parentHandle);
            }else
            {
                log.log("Indo App Already exist on Users Fb acct ... Move to next screen");
                //driver.switchTo().window(parentHandle);
            }
            log.log("END....... ");
            break;
        }


    }
    //log.log("Indo App Already exist on Users Fb acct ... Move to next screen");
    //log.log("Element NOT present ... Move to next screen");   
    //Thread.sleep(3000);       





    Thread.sleep(2000);
    log.divider("Check if User is Signed In already with a different account or NOT........ pls wait");
    driver.switchTo().window(parentHandle);

    //WebElement signedInAlready = driver.findElement(By.cssSelector(CONSTANTs.ALREADY_SIGNED_UP_SCREEN));      
    //String ggg = signedInAlready.getText();

    String signedInAlready = "";
    signedInAlready = driver.getPageSource();
    Thread.sleep(2000);
    if(signedInAlready.contains("Already Signed up?"))
    {

    log.log("User is Signed in Already with another account");
    Thread.sleep(3000);
    driver.findElement(By.cssSelector(CONSTANTs.ALREADY_SIGNED_UP_PASSWD_TXTBOX)).sendKeys(CONSTANTs.NATIVE_PASSWORD);
    Thread.sleep(2000);
    driver.findElement(By.cssSelector(CONSTANTs.ALREADY_SIGNED_UP_BUTTON)).click();
    Thread.sleep(2000);
    log.log("User successfully logged in, ... Linked Account together.....");
    }
    else 
    {
    log.log("Successfully Logged in Using FB.");}       
}

问题是当代码 运行s 第一次出现时 optionalClick(By.cssSelector(CONSTANTs.OK_CONTINUE_AS)) 此代码 运行s 作为元素存在。但是当代码 运行 第二次出现时,optionalClick(By.cssSelector(CONSTANTs.OK_CONTINUE_AS)) 这段代码不应该 运行 因为元素不存在。

现在我想让 else 工作,但我遇到了冻结。我认为我的问题是在屏幕之间切换,如果是对的但我在这里完全困惑。

对于 optionalClick 方法,使用 explicit wait。如果在 10 秒内找到元素(根据代码),将单击元素。否则会引发超时异常并在 catch 块中处理。

try{
   (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(by)));
   driver.findElement(by).click();
   }
catch(TimeoutException te){
}