トップページ > >Selenium API(逆引き) > 【Ruby】window_handles

【Ruby】window_handles・・・複数のウインドウハンドルを取得する

【PR】業界トップクラスの「高単価報酬」「低マージン」を実現!レバテックフリーランス

「window_handles」実行で複数のウインドウハンドルを取得できます。

カレントウインドウ(制御対象になっているウインドウ)のハンドルのみを取得する「current_window_handle」と異なり「window_handles」はカレントセッション(指定したインスタンス)の全てのハンドルを取得していきます。そして複数取得したウインドウハンドルをswitch_to.window(xxx)で指定することで制御したいウインドウ(ブラウザ)を切り替えることが出来ます。

「window_handles」のウインドウハンドル取得順序ですが、最初にインスタンスウインドウ(一番初めに開いたウィンドウ)のウインドウハンドルを取得し、それ以降は最も新しい(最後に開いた)ウインドウからウインドウハンドルを取得していきます。

下記サンプルでは、まず初めにインスタンスウインドウとなるgetWindowHandles.html<図1>を開いています。次に開いたインスタンスウインドウから各サイトへ複数の新規ウインドウ開いています。そしてそれぞれのウインドウハンドルを取得し、ウインドウハンドルをswitch_to.window(xxx)で指定することで個別のウインドウ制御を可能にしています。

説明

◆メソッド
  ・window_handles
◆使用形態
  ・driver.window_handles
◆備考
  ・window_handles実行でまず初めにインスタンスウインドウのウインドウハンドル
   を取得し、それ以降は新しいウインドウからウインドウハンドルを取得していく
◆関連項目
  ・全てのクッキーを取得する
  ・クッキー名からクッキーを取得する

サンプル

require 'selenium-webdriver'

# Firefoxを起動
driver = Selenium::WebDriver.for(:firefox)
#指定したURLに遷移<図1>
driver.get('file://D:/AutoTest/getWindowHandles.html')
#それぞれのテキスト名のリンク要素を取得
link1 = driver.find_element(:partial_link_text, 'Yahoo Top')
link2 = driver.find_element(:partial_link_text, 'Firefox Top')
link3 = driver.find_element(:partial_link_text, 'Opera Top')
#ローカルファイル内のYahooへのリンクをクリックし新規ウィンドウを立ち上げる
link1.click
#ローカルファイル内のFirefoxへのリンクをクリックし新規ウィンドウを立ち上げる
link2.click
#ローカルファイル内のOperaへのリンクをクリックし新規ウィンドウを立ち上げる
link3.click
#全てのウィンドウハンドルを取得
allHandles = driver.window_handles
#ウィンドウハンドル1
puts(allHandles[0])
#ウィンドウハンドル2
puts(allHandles[1])
#ウィンドウハンドル3
puts(allHandles[2])
#ウィンドウハンドル4
puts(allHandles[3])
#getWindowHandles.htmlの内容からMSNページへ移行した
driver.switch_to.window(allHandles[0])
driver.get('https://www.msn.com')
#OperaページからGoogleページへ移行した
driver.switch_to.window(allHandles[1])
driver.get('https://www.google.com')
#FirefoxページからMicrosoftページへ移行した
driver.switch_to.window(allHandles[2])
driver.get('https://www.microsoft.com')
※selenium version 4.1.0で動作確認をしています

実行結果

1234593452
1234534689
1234534557
1234534555
※ここで設定されているウインドウハンドルの内容は架空の値です

getWindowHandles.html<図1>

<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="UTF-8">
        <title>SeleniumTest</title>
    </head>
    <body>
        <div>
            <a href="https://www.yahoo.co.jp/" target="YAHOO">Yahoo Top</a>
        </div>
        <div>
            <a href="https://www.firefox.com/" target="FIREFOX">Firefox Top</a>
        </div>
        <div>
            <a href="https://www.opera.com/ja" target="OPERA">Opera Top</a>
        </div>
    </body>
</html>

動画デモ

※字幕をONにすると解説のテロップが表示されます※


Fatal error: Uncaught Google_Service_Exception: Error calling GET https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A138132118&start-date=7daysAgo&end-date=yesterday&metrics=ga%3Apageviews&dimensions=ga%3ApageTitle%2C+ga%3ApagePath&sort=-ga%3Apageviews&max-results=1000: (403) User does not have sufficient permissions for this profile. in /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php:110 Stack trace: #0 /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client)) #1 /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Task/Runner.php(174): Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request)) #2 /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php(46): Google_Task_Runner->run() #3 /home/users/1/monda-muki/web/seleniumqref in /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php on line 110