17

これは私のコードです:

driver.findElement(By.id("ImageButton5")).click();
//Thread.sleep(3000);
String winHandleBefore = driver.getWindowHandle();
driver.switchTo().window(winHandleBefore);
driver.findElement(By.id("txtEnterCptCode")).sendKeys("99219");

今、私は次のエラーがあります:

スレッド「メイン」org.openqa.selenium.NoSuchElementException での例外: ID == txtEnterCptCode の要素が見つかりません (警告: サーバーはスタックトレース情報を提供しませんでした) コマンド期間またはタイムアウト: 404 ミリ秒。

何か案は?

4

6 に答える 6

41

実際には新しいウィンドウに切り替えていないようです。元のウィンドウのウィンドウ ハンドルを取得して保存し、新しいウィンドウのウィンドウ ハンドルを取得してそれに切り替えます。新しいウィンドウの操作が完了したら、それを閉じる必要があります。その後、元のウィンドウ ハンドルに戻ります。以下のサンプルを参照してください。

すなわち

String parentHandle = driver.getWindowHandle(); // get the current window handle
driver.findElement(By.xpath("//*[@id='someXpath']")).click(); // click some link that opens a new window

for (String winHandle : driver.getWindowHandles()) {
    driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
}

//code to do something on new window

driver.close(); // close newly opened window when done with it
driver.switchTo().window(parentHandle); // switch back to the original window
于 2013-10-01T12:20:25.883 に答える
2

以下に示すように、必要なウィンドウに切り替えるユーティリティメソッドがあります

public class Utility 
{
    public static WebDriver getHandleToWindow(String title){

        //parentWindowHandle = WebDriverInitialize.getDriver().getWindowHandle(); // save the current window handle.
        WebDriver popup = null;
        Set<String> windowIterator = WebDriverInitialize.getDriver().getWindowHandles();
        System.err.println("No of windows :  " + windowIterator.size());
        for (String s : windowIterator) {
          String windowHandle = s; 
          popup = WebDriverInitialize.getDriver().switchTo().window(windowHandle);
          System.out.println("Window Title : " + popup.getTitle());
          System.out.println("Window Url : " + popup.getCurrentUrl());
          if (popup.getTitle().equals(title) ){
              System.out.println("Selected Window Title : " + popup.getTitle());
              return popup;
          }

        }
                System.out.println("Window Title :" + popup.getTitle());
                System.out.println();
            return popup;
        }
}

ウィンドウのタイトルがパラメーターとして渡されると、目的のウィンドウに移動します。あなたの場合、あなたはすることができます。

Webdriver childDriver = Utility.getHandleToWindow("titleOfChildWindow");

同じ方法を使用して親ウィンドウに再度切り替えます

Webdriver parentDriver = Utility.getHandleToWindow("titleOfParentWindow");

この方法は、複数のウィンドウを扱う場合に効果的です。

于 2013-10-01T09:50:18.250 に答える
1
Set<String> windows = driver.getWindowHandles();
Iterator<String> itr = windows.iterator();

//patName will provide you parent window
String patName = itr.next();

//chldName will provide you child window
String chldName = itr.next();

//Switch to child window
driver.switchto().window(chldName);

//Do normal selenium code for performing action in child window

//To come back to parent window
driver.switchto().window(patName);
于 2014-11-11T07:40:13.543 に答える
0

このためのサンプルプログラムがあります:

public class BrowserBackForward {

/**
 * @param args
 * @throws InterruptedException 
 */
public static void main(String[] args) throws InterruptedException {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://seleniumhq.org/");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    //maximize the window
    driver.manage().window().maximize();

    driver.findElement(By.linkText("Documentation")).click();
    System.out.println(driver.getCurrentUrl());
    driver.navigate().back();
    System.out.println(driver.getCurrentUrl());
    Thread.sleep(30000);
    driver.navigate().forward();
    System.out.println("Forward");
    Thread.sleep(30000);
    driver.navigate().refresh();

}

}

于 2013-10-01T12:21:08.900 に答える