切换到新窗口后,驱动程序无法找到元素

时间:2015-07-21 14:33:18

标签: java selenium selenium-webdriver junit

我有一个按钮的页面,按下后会打开一个新的弹出窗口。 我可以成功导航到弹出窗口,但驱动程序仍然会抛出错误:

  

没有这样的元素

代码如下:

    String pwindow = this.getDriver().getWindowHandle();
    genPage.clickByXpath("/html/body/div/div[2]/table/tbody/tr/td[2]/form/div[1]/div/div/div/div/div[2]/div/div/table/tbody/tr[1]/td[2]/div/div[2]/span/a/img");

    Set<String> allWindows = this.getDriver().getWindowHandles();
    System.out.println(allWindows);
    String[] windows = allWindows.toArray(new String[allWindows.size()]);
    System.out.println("[" + windows[0] + ", " + windows[1] + ", " + windows[windows.length - 1] + "]");

    System.out.println("Title : " +  this.getDriver().getTitle());
    System.out.println("Current Url : " + this.getDriver().getCurrentUrl());
    System.out.println("Window : " + this.getDriver().getWindowHandle());
    System.out.println("Parent Window : " + pwindow);
    System.out.print("\n");

    this.getDriver().switchTo().window(windows[windows.length - 1]);
    //this.getDriver().switchTo().defaultContent();
    //this.getDriver().switchTo().frame("searchFrame");
    // ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=searchFrame | ]]
    // ERROR: Caught exception [ERROR: Unsupported command [waitForPopUp | lookup | 30000]]

    System.out.println("Title : " +  this.getDriver().getTitle());
    System.out.println("Current Url : " + this.getDriver().getCurrentUrl());
    System.out.println("Window : " + this.getDriver().getWindowHandle());

    genPage.insertById("id", "keys"); //cant find this element or any after
    genPage.clickByName("go");
    genPage.clickByLinkText("Name");

    this.getDriver().switchTo().window(pwindow);

genPage的方法:

public void clickByXpath(String xpath){
    WebElement ele;
    ele = this.getDriver().findElement(By.xpath(xpath));
    ele.click();
}

public void clickByLinkText(String link){
    WebElement ele;
    ele = this.getDriver().findElement(By.linkText(link));
    ele.click();
}

public void clickByName(String name){
    WebElement ele;
    ele = this.getDriver().findElement(By.name(name));
    ele.click();
}

public void insertById(String id, String data){
    WebElement ele;
    ele = this.getDriver().findElement(By.id(id));
    ele.sendKeys(data);
}

任何有关错误或错过的帮助都将不胜感激。

由于

斯科特

0 个答案:

没有答案