0

ウィンドウハンドルを使用してポップウィンドウを操作することについて話している多数の投稿を見てきました。しかし、ボタンをクリックすると開くブラウザで作業する方法 -

<button class="power_buy_now_button" type="button"> 
</button>

ウィンドウハンドルを取得しようとしましたが、文字列が変化するたびに、「8c5f028e-e7cc-4d0f-afe4-983bb119391e」のようなものになります

新しいブラウザに関連付けられたタイトルすらありません。さらに、タイトルを使用して新しいブラウザーを制御する方法がわかりません。そしてある時点で、制御を最初のブラウザに戻す必要があります。

4

2 に答える 2

4

あなたの最善の策は、次のようなことをすることです:

// This code assumes you start with only one browser window in your test.
// If you have more than one browser window, your code will be more complex.
string originalHandle = driver.GetWindowHandle();
driver.FindElement(By.ClassName("power_buy_now_button")).Click();

// May need to wait for window handles collection to have a .Count of 2,
// as clicks are asynchronous.
string popupHandle = string.Empty;
ReadOnlyCollection<string> windowHandles = driver.GetWindowHandles();
foreach (string handle in windowHandles)
{
    if (handle != originalHandle)
    {
        popupHandle = handle;
        break;
    }
}

driver.SwitchTo().Window(popupHandle);
// Do stuff in the popup window, eventually closing it with driver.Close()
driver.SwitchTo().Window(originalHandle);
于 2011-04-13T19:38:00.183 に答える
0

私は2番目のハンドルを選んでそれをクリックすることになりました。このアプローチは機能し、メインウィンドウにも制御を戻すことができることを願っています。

于 2011-03-31T18:44:51.540 に答える