2012-02-08 5 views
21

セレンウェブドライバを使ってWebアプリケーションを自動化する際に問題があります。セレンウェブドライバは、新しいウィンドウが開いた時点を知り、実行を再開する方法を教えてください。

ウェブページにはボタンがあり、クリックすると新しいウィンドウが開きます。私は次のコードを使用すると、それは上記の問題を解決するためにOpenQA.Selenium.NoSuchWindowException: No window found

WebDriver.FindElement(By.Id("id of the button that opens new window")).Click(); 
//Switch to new window 
_WebDriver.SwitchTo().Window("new window name"); 
//Click on button present on the newly opened window 
_WebDriver.FindElement(By.Id("id of button present on newly opened window")).Click(); 

を投げる私は、ボタンのクリックとSwitchTo文の間Thread.Sleep(50000);を追加します。

WebDriver.FindElement(By.Id("id of the button that opens new window")).Click(); 
Thread.Sleep(50000); //wait 
//Switch to new window 
_WebDriver.SwitchTo().Window("new window name"); 
//Click on button present on the newly opened window 
_WebDriver.FindElement(By.Id("id of button present on newly opened window")).Click(); 

それは問題を解決しますが、ウィンドウを開くにはもっと時間がかかる場合、コードは失敗し、ウィンドウをすばやく開くならば、それはテストが必要以上に遅くなりますので、私はThread.Sleep(50000);ステートメントを使用する必要はありません。

ウィンドウが開いてテストが実行を再開できるかどうかを知る方法はありますか?操作はPythonで、例えば成功するまで

答えて

25

あなたがポップアップするウィンドウをそれに任意の操作を行う前に制御を切り替える必要があります。これを使うことで、あなたの問題を解決することができます。

ポップアップウィンドウを開く前に、メインウィンドウのハンドルを取得して保存します。

String mwh=driver.getWindowHandle();

現在、いくつかのアクションを実行することにより、ポップアップウィンドウを開こう:

driver.findElement(By.xpath("")).click(); 

Set s=driver.getWindowHandles(); //this method will gives you the handles of all opened windows 

Iterator ite=s.iterator(); 

while(ite.hasNext()) 
{ 
    String popupHandle=ite.next().toString(); 
    if(!popupHandle.contains(mwh)) 
    { 
     driver.switchTo().window(popupHandle); 
     /**/here you can perform operation in pop-up window** 
     //After finished your operation in pop-up just select the main window again 
     driver.switchTo().window(mwh); 
    } 
} 
+0

返事ありがとうございます。それは働いています。 – Ozone

+1

新しいタブが開かれているがドライブインスタンスにハンドルが追加されていない場合があります。私の解決策はクリックする前に現在のハンドル数を取得してからwhileループ中にカウントが変更されたかどうかをチェックします。その後、この 'driver.switchTo()。window(handles [handles.count() - 1]);'のように新しく開かれたタブに切り替えます。 ''ハンドル 'は各繰り返しで更新されます。 – Edgar

9

あなたは待つことができる:

from selenium.common.exceptions import NoSuchWindowException 
from selenium.webdriver.support.ui import WebDriverWait 

def found_window(name): 
    def predicate(driver): 
     try: driver.switch_to_window(name) 
     except NoSuchWindowException: 
      return False 
     else: 
      return True # found window 
    return predicate 

driver.find_element_by_id("id of the button that opens new window").click()   
WebDriverWait(driver, timeout=50).until(found_window("new window name")) 
WebDriverWait(driver, timeout=10).until(# wait until the button is available 
    lambda x: x.find_element_by_id("id of button present on newly opened window"))\ 
    .click() 
+0

おかげコードでこのメソッドを呼び出します。 – Ozone

1

私は最終的に

、私は新しいウィンドウに切り替えるには、以下の方法を使用 、答えを見つけました
public String switchwindow(String object, String data){ 
     try { 

     String winHandleBefore = driver.getWindowHandle(); 

     for(String winHandle : driver.getWindowHandles()){ 
      driver.switchTo().window(winHandle); 
     } 
     }catch(Exception e){ 
     return Constants.KEYWORD_FAIL+ "Unable to Switch Window" + e.getMessage(); 
     } 
     return Constants.KEYWORD_PASS; 
     } 

親ウィンドウに移動するには、次のコードを使用しました。

public String switchwindowback(String object, String data){ 
      try { 
       String winHandleBefore = driver.getWindowHandle(); 
       driver.close(); 
       //Switch back to original browser (first window) 
       driver.switchTo().window(winHandleBefore); 
       //continue with original browser (first window) 
      }catch(Exception e){ 
      return Constants.KEYWORD_FAIL+ "Unable to Switch to main window" + e.getMessage(); 
      } 
      return Constants.KEYWORD_PASS; 
      } 

これは、ウィンドウ間の切り替えに役立つと思います。

1

これを使用して、ウィンドウを開くまで待つことができます。

C#コード:

public static void WaitUntilNewWindowIsOpened(this RemoteWebDriver driver, int expectedNumberOfWindows, int maxRetryCount = 100) 
    { 
     int returnValue; 
     bool boolReturnValue; 
     for (var i = 0; i < maxRetryCount; Thread.Sleep(100), i++) 
     { 
      returnValue = driver.WindowHandles.Count; 
      boolReturnValue = (returnValue == expectedNumberOfWindows ? true : false); 
      if (boolReturnValue) 
      { 
       return; 
      } 
     } 
     //try one last time to check for window 
     returnValue = driver.WindowHandles.Count; 
     boolReturnValue = (returnValue == expectedNumberOfWindows ? true : false); 
     if (!boolReturnValue) 
     { 
      throw new ApplicationException("New window did not open."); 
     } 
    } 

そして私は、返信用

Extensions.WaitUntilNewWindowIsOpened(driver, 2); 
関連する問題