2

編集4:

ここに画像の説明を入力 編集3 ここに画像の説明を入力

編集2

    string currentWindow = driver.CurrentWindowHandle;

    driver.SwitchTo().Window("");
    string childTitle = driver.Title;

    driver.SwitchTo().Window(currentWindow);
    string parentTitle = driver.Title;

上記のコードは、親ウィンドウまたは子ウィンドウに同じタイトルを付けます。

編集:

<a id="ctl00_ctl00_Features_ctl03_lnkPage" class="title" target="_blank" href="websiteaddress">Stay  Around</a>

新しく開いたウィンドウのタイトルを確認し、確認したら開いた新しいウィンドウを閉じる方法は?

私のページにはリンクがあり、リンクをクリックすると新しいウィンドウが開きますが、そのウィンドウのタイトルを確認する方法がわかりません。

これが私がこれまでに行ったことです。

GoToMysiteUrl();
IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
addtoList.Click();

//新しいウィンドウを開く

ここで、新しいウィンドウにフォーカスを切り替えてタイトルを確認し、新しいウィンドウを閉じて前のウィンドウに戻したいと思います。

4

2 に答える 2

2

IE でポップアップ ウィンドウを扱うときにほとんどの人が見逃しているのは、要素のクリックが非同期であることです。つまり.WindowHandles、クリックした直後にプロパティをチェックすると、競合状態が失われる可能性があります。これは、IE が新しいウィンドウを作成する機会を得る前に新しいウィンドウの存在をチェックしているためであり、ドライバーが登録するチャンスが存在します。

同じ操作を実行するために使用する C# コードを次に示します。

string foundHandle = null;
string originalWindowHandle = driver.CurrentWindowHandle;

// Get the list of existing window handles.
IList<string> existingHandles = driver.WindowHandles;
IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
addtoList.Click();

// Use a timeout. Alternatively, you could use a WebDriverWait
// for this operation.
DateTime timeout = DateTime.Now.Add(TimeSpan.FromSeconds(5));
while(DateTime.Now < timeout)
{
    // This method uses LINQ, so it presupposes you are running on
    // .NET 3.5 or above. Alternatively, it's possible to do this
    // without LINQ, but the code is more verbose.
    IList<string> currentHandles = driver.WindowHandles;
    IList<string> differentHandles = currentHandles.Except(existingHandles).ToList();
    if (differentHandles.Count > 0)
    {
        // There will ordinarily only be one handle in this list,
        // so it should be safe to return the first one here.
        foundHandle = differentHandles[0];
        break;
    }

    // Sleep for a very short period of time to prevent starving the driver thread.
    System.Threading.Thread.Sleep(250);
}

if (string.IsNullOrEmpty(foundHandle))
{
    throw new Exception("didn't find popup window within timeout");
}

driver.SwitchToWindow(foundHandle);

// Do whatever verification on the popup window you need to, then...
driver.Close();

// And switch back to the original window handle.
driver.SwitchToWindow(originalWindowHandle);

ちなみに、.NET バインディングを使用している場合はPopupWindowFinder、WebDriver.Support.dll アセンブリ内のクラスにアクセスできます。これは、ポップアップ ウィンドウを見つけるのと非常によく似た方法を使用します。クラスがニーズを正確に満たし、変更せずに使用できることがわかる場合があります。

于 2012-11-06T19:33:55.257 に答える
0
GoToMysiteUrl();
IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
addtoList.Click();

// 上記の操作を投稿すると、問題で説明されているように新しいウィンドウが開きます

// メイン ウィンドウのハンドルを取得します

string  currentWindow = Driver.CurrentWindowHandle;

// 新しく開いたウィンドウに切り替える

Driver.SwitchTo().Window("Your Window Name");

// ここで必要なアクション/アサーションを実行し、ウィンドウを閉じます

// メインウィンドウに切り替え

Driver.SwitchTo().Window(currentWindow);
于 2012-11-06T08:27:57.487 に答える