我在IE Webdriver中执行switchTo(new Handle)后面临在新窗口中查找元素的问题

时间:2016-10-28 06:43:01

标签: java selenium

我从主函数中获取帐户的值。 我认为元素在框架内。你能帮帮我吗?

我正在使用的代码是

public static void FSVLogin(WebDriver driver){

    try{

        driver.findElement(By.xpath("//*[@id='loginMode_6']")).click();
        Thread.sleep(3000);

        driver.findElement(By.xpath("//*[@id='btnLaunch']")).click();
        Thread.sleep(6000);

        switchwindow(driver);

    }catch(Exception e){
        System.out.println(e);

    }

}



public static void switchwindow(WebDriver driver){

    //Get all window handles
    Set<String> allHandles = driver.getWindowHandles();

    //count the handles Here count is=2
    System.out.println("Count of windows:"+allHandles.size());  

  //Get current handle or default handle
    String currentWindowHandle = allHandles.iterator().next();
    System.out.println("currentWindow Handle"+currentWindowHandle);

  //Remove first/default Handle
    allHandles.remove(allHandles.iterator().next());

  //get the last Window Handle
    String lastHandle = allHandles.iterator().next();
    System.out.println("last window handle"+lastHandle);

  //switch to second/last window, because we know there are only two    windows 1-parent window 2-other window(ad window)
    driver.switchTo().window(lastHandle);
    System.out.println(driver.getTitle());

}


public static void LoadAccountCase(WebDriver driver,String account,String caseid){


try{
    System.out.println("inside account case load function");

    //WebElement card=driver.findElement(By.xpath("//*[@id='txtCardNumber']"));
    String id="txtCardNumber";
    waitForPageLoad(driver,id);
    driver.findElement(By.id("txtCardNumber")).click();
    driver.findElement(By.id("txtCardNumber")).sendKeys(account);

    }catch(Exception e){
        System.out.println(e);

    }



}

1 个答案:

答案 0 :(得分:0)

如果新窗口中的元素位于Frame内,那么首先需要切换到该框架,然后找到该元素。

frame = driver.findElement(by.id("id of the frame"));
driver.switchTo().frame(frame);

/*To switch back to the page use*/
driver.switchTo().defaultContent();