selenium タブ切り替え

PC

Seleniumでサイトを巡回させたり、何かを入力したり。
プログラムで色々なサイトを回らせるわけですが、単発司令のものをループで走らせると、その都度新ウィンドウを開ける仕様なので、気づいたらものすごい数のウィンドウが画面を埋め尽くしたりしてました。
で、これはさすがにまずかろうというので、新規タブで開けるようにしてみたのですが、、、まあ、限度がありますよね。
数が多いと、タブを切り替えてどうにかするとかいうレベルじゃなくなるので。

というわけで、適宜タブを閉じて新規タブを開けて作業させる、みたいなことを検討してみました。

利用使途としては、例えばGoogleさんで検索結果の一覧を出しておきながら、違うタブで各検索順位のサイトを徘徊して、必要な情報を取っては戻る、みたいなことですね。
そこでのSeleniumプログラムは、検索一覧のタブは残しておきながら、徘徊先サイトは別タブでその都度開けては閉めるを繰り返す、という流れになります。

問題は、タブ名を自動では割り振ってくれない点と、Seleniumプログラムが見ているタブが視覚的に見えるタブと違う点。
それぞれ、プログラム上でその都度具体的に指示してあげないとうまく動いてくれないのですね。

まず、タブ名の割り振りの件。
自動では割り振られないので、指示を出します。
driver.window_handles」を使うことで、タブに番号が振られます。
list型の配列になります。

なので、
driver.window_handles[0] と書くと最初のタブ、
driver.window_handles[1] と書くとその次のタブが呼び出されることになります。

それじゃあ、ということで、

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
tabs = driver.window_handles
tabs = driver.window_handles
tabs = driver.window_handles

とかおいて使いまわそうとしたのですが、格納するのはその時点のウィンドウ/タブだけなんですね。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
tabs = driver.window_handles
# tab_0
tabs[0] = driver.get('https://www.google.com/search?q=あああ')
# tab_1
driver.execute_script("window.open('');")
driver.switch_to.window(tabs[1])
tabs[1] = driver.get('https://www.google.com/search?q=いいい')
tabs = driver.window_handles # tab_0 tabs[0] = driver.get('https://www.google.com/search?q=あああ') # tab_1 driver.execute_script("window.open('');") driver.switch_to.window(tabs[1]) tabs[1] = driver.get('https://www.google.com/search?q=いいい')
tabs = driver.window_handles

# tab_0
tabs[0] = driver.get('https://www.google.com/search?q=あああ')

# tab_1
driver.execute_script("window.open('');")
driver.switch_to.window(tabs[1])
tabs[1] = driver.get('https://www.google.com/search?q=いいい')

こうすると、8行目「driver.switch_to.window(tabs[1])」のところで、

IndexError: list index out of range

リストからはみ出してるやんけ、と言われるのです。
対処としては、面倒なのですが新しいウィンドウ・タブを開けるたびに「driver.window_handles」を振り直すか、もう初めから置き換えるのは諦めるか、です。

具体的には、tabs[1]を使わず、driver.window_handles[1]とするか、
新規タブを開くたびに呪文のように、
tabs = driver.window_handles
と振り直すか、ですね。

次、閉じたタブがまだ操作対象になっている件。
タブを閉じて元のタブに戻ってそこでまた指示を出すという場合、閉じただけではプログラム上は操作対象のタブは切り替わっていません。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
driver.close()
tabs[0] = driver.get('https://www.google.com/search?q=ううう')
driver.close() tabs[0] = driver.get('https://www.google.com/search?q=ううう')
driver.close()
tabs[0] = driver.get('https://www.google.com/search?q=ううう')

とすると、

no such window: target window already closed

そのウィンドウもう閉じてまっせ、と言われます。
閉じたら、対象タブも動かさないといけません。

上記の2文の間に、

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
driver.switch_to.window(tabs[0])
driver.switch_to.window(tabs[0])
driver.switch_to.window(tabs[0])

を突っ込んであげます。
この場合は、リストの中のウィンドウ/タブが増えたわけじゃないので、改めて番号を振り直さないでも文句言わずに稼働してくれます。

というわけで

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# tab_0
tabs = driver.window_handles
tabs[0] = driver.get('https://www.google.com/search?q=あああ')
# tab_1
driver.execute_script("window.open('');")
tabs = driver.window_handles
driver.switch_to.window(tabs[1])
tabs[1] = driver.get('https://www.google.com/search?q=いいい')
# tab_1を閉じ、tab_0 に戻ってゴニョゴニョ。
driver.close()
driver.switch_to.window(tabs[0])
tabs[0] = driver.get('https://www.google.com/search?q=ううう')
# tab_0 tabs = driver.window_handles tabs[0] = driver.get('https://www.google.com/search?q=あああ') # tab_1 driver.execute_script("window.open('');") tabs = driver.window_handles driver.switch_to.window(tabs[1]) tabs[1] = driver.get('https://www.google.com/search?q=いいい') # tab_1を閉じ、tab_0 に戻ってゴニョゴニョ。 driver.close() driver.switch_to.window(tabs[0]) tabs[0] = driver.get('https://www.google.com/search?q=ううう')
# tab_0
tabs = driver.window_handles
tabs[0] = driver.get('https://www.google.com/search?q=あああ')

# tab_1
driver.execute_script("window.open('');")
tabs = driver.window_handles
driver.switch_to.window(tabs[1])
tabs[1] = driver.get('https://www.google.com/search?q=いいい')

# tab_1を閉じ、tab_0 に戻ってゴニョゴニョ。
driver.close()
driver.switch_to.window(tabs[0])

tabs[0] = driver.get('https://www.google.com/search?q=ううう')

こんな感じで。

1枚めのタブを開くときの番号振りは冗長なので、2,3行目は単に

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
driver.get('https://www.google.com/search?q=あああ')
driver.get('https://www.google.com/search?q=あああ')
driver.get('https://www.google.com/search?q=あああ')

だけでも問題ないですけどね。

PythonのSelenium本

Hands-On Web Scraping with Python Perform advanced scraping operations using various Python librarie
Hands-On Web Scraping with Python Perform advanced scraping operations using various Python librarie 4,304円(税込)【送料込】

楽天Kobo電子書籍ストア

<p><b>Collect and scrape different complexities of data from the modern Web using the latest tools,

Test-Driven Development with Python Obey the Testing Goat: Using Django, Selenium, and JavaScript【電子
Test-Driven Development with Python Obey the Testing Goat: Using Django, Selenium, and JavaScript【電子 3,529円(税込)【送料込】

楽天Kobo電子書籍ストア

<p>By taking you through the development of a real web application from beginning to end, the second

Selenium自?化??完全指南:基于Python【電子書籍】
Selenium自?化??完全指南:基于Python【電子書籍】 7,020円(税込)【送料込】

楽天Kobo電子書籍ストア

<p>本?共有19章。第1〜10章介?Selenium IDE、Selenium WebDriver、Selenium Grid、Appium等工具的?用。第11〜16章介?自?化????的??及模式

Python??WebUI自?化????ーーSelenium3/4+unittest/Pytest+GitLab+Jenkins【電子書籍】[ Storm ]
Python??WebUI自?化????ーーSelenium3/4+unittest/Pytest+GitLab+Jenkins【電子書籍】[ Storm ] 7,083円(税込)【送料込】

楽天Kobo電子書籍ストア

<p>本?主要介?如何基于Python使用Selenium、unittest、Pytest、GitLab、Jenkins等工具??Web UI自?化??,以?助?者提升??水平。本?第1章?要介?自?

洋書 Paperback, Test-Driven Development with Python: Obey the Testing Goat: Using Django, Selenium, an
洋書 Paperback, Test-Driven Development with Python: Obey the Testing Goat: Using Django, Selenium, an 15,783円(税込)【送料別】

Glomarket

*** We ship internationally, so do not use a package forwarding service. We cannot ship to a package

楽天ウェブサービスセンター
タイトルとURLをコピーしました