如果下一个元素不存在,则打破for循环

时间:2016-01-08 10:44:41

标签: java selenium webdriver

/**
 * 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.");}       
}

我遇到的问题是当代码第一次运行时optionalClick(By.cssSelector(CONSTANTs.OK_CONTINUE_AS))此代码在元素存在时运行。但是当代码第二次运行时,optionalClick(By.cssSelector(CONSTANTs.OK_CONTINUE_AS))此代码不应该运行,因为该元素将不存在。

现在我希望其他人工作,但我正在冻结。我认为我的问题是在屏幕之间切换,如果我是对的,但我在这里完全混淆。

1 个答案:

答案 0 :(得分:0)

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

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