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

【Python】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実行でまず初めにインスタンスウインドウのウインドウハンドル
   を取得し、それ以降は新しいウインドウからウインドウハンドルを取得していく
◆関連項目
  ・ウィンドウのサイズを取得する
  ・全てのクッキーを取得する

サンプル

from selenium import webdriver
from selenium.webdriver.chrome import service
from selenium.webdriver.common.by import By

#ChromeDriverのパスを変数に設定
CHROMEDRIVER = "D:\driver\chromedriver.exe"
#ChromeDriverのstartとstopを制御するServiceオブジェクトを介してパスを渡す
chrome_service = service.Service(executable_path=CHROMEDRIVER)
#Chromeを起動
driver = webdriver.Chrome(service=chrome_service)
#指定したURLに遷移<図1>
driver.get("file://D:/AutoTest/getWindowHandles.html")
#それぞれのテキスト名のリンク要素を取得
link1 = driver.find_element(By.LINK_TEXT, "Yahoo Top")
link2 = driver.find_element(By.LINK_TEXT, "Firefox Top")
link3 = driver.find_element(By.LINK_TEXT, "Opera Top")
#ローカルファイル内のYahooへのリンクをクリックし新規ウィンドウを立ち上げる
link1.click()
#ローカルファイル内のFirefoxへのリンクをクリックし新規ウィンドウを立ち上げる
link2.click()
#ローカルファイル内のOperaへのリンクをクリックし新規ウィンドウを立ち上げる
link3.click()
#全てのウィンドウハンドルを取得
allHandles = driver.window_handles
#ウィンドウハンドル1
print(allHandles[0])
#ウィンドウハンドル2
print(allHandles[1])
#ウィンドウハンドル3
print(allHandles[2])
#ウィンドウハンドル4
print(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.3で動作確認をしています(ブラウザのUIや属性値などが変更された場合、実行結果が異なる可能性があります)

実行結果

CDwindow-12345678-45ad-4676-83a5-da4ebcefa127
CDwindow-87654321-ff35-4244-b4c5-1a3b3678c564
CDwindow-98765432-add2-487f-a985-68c5b999a77a
CDwindow-67895432-adea-452e-b6a9-3ad7918872d5
※ここで設定されているウインドウハンドルの内容は架空の値です

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にすると解説のテロップが表示されます※


Warning: Invalid argument supplied for foreach() in /home/users/1/monda-muki/web/seleniumqref.com/api/python/window_get/Python_window_handles.html on line 217

PythonAPIアクセス TOP10
過去1週間(3/21~3/27)

    カテゴリー

    環境構築

    APIリファレンス

    その他

    ページ上部へ戻る
    トップページ > >Selenium API(逆引き) > 【Python】window_handles
    Copyright © 2016- Seleniumクイックリファレンス All Rights Reserved