実際、Javaを使用してSelenium Automationで現在のウィンドウを閉じた後、現在のウィンドウから既存のウィンドウにコントロールを切り替えようとしています。それを行う方法はありますか。新しく開いたウィンドウを制御し、いくつかのプロセスを実行してこれを閉じることができます。後で、既存のブラウザ ウィンドウに移動するだけです。
4571 次
6 に答える
1
これは私が使用するもので、開いているすべてのウィンドウをチェックし、古いウィンドウを閉じるオプションを使用して次のウィンドウに制御を切り替えます。
protected final void switchWindows(boolean closeOldWindow) {
final WebDriver driver = checkNotNull(getDriver(), "missing WebDriver");
final String currentWindow = driver.getWindowHandle();
checkNotNull(currentWindow);
// switch to first window that is not equal to the current window
String newWindow = null;
for (final String handle : driver.getWindowHandles()) {
if (!currentWindow.equals(handle)) {
newWindow = handle;
break;
}
}
// if there's another window found...
if (newWindow != null) {
if (closeOldWindow) {
// close the current window
driver.close();
}
// ...switch to the new window
driver.switchTo().window(newWindow);
}
}
于 2013-10-25T21:07:51.397 に答える
0
Selenium js webdriverには、driver.switchTo().window(windowName)
別のウィンドウに移動するのに役立つようなAPIがあることを知っています。私はJava APIにあまり詳しくありませんが、それらはすべてほぼ同じ呼び出し方法からのものです。これがあなたを助けることを願っています。
于 2013-10-22T14:17:03.380 に答える
0
以下に示すように、必要なウィンドウに切り替えるユーティリティメソッドがあります
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-27T06:03:06.840 に答える