3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Python]Seleniumでタブ移動する方法

Posted at

結論

driver.window_handles[タブの順番]を操作することで実現できる。

コード例
driver = webdriver.Chrome(chrome_options=op)
driver.get(home_url)

# 新しいタブが出来るとする
driver.find_element_by_id("move-tab")
driver.click()

# 新しくできたタブに移動して元のタブを閉じる動作
# driver.window_handles[タブの番号]
driver.switch_to.window(driver.window_handles[1])
driver.switch_to.window(driver.window_handles[0])
# 初めに開いていたウインドウを閉じる
driver.close()
# 新しく開かれたタブを新規にインスタンスウインドウとする これをしないとNosuchElementエラーになる
driver.switch_to.window(driver.window_handles[0])

参考文献

[How to switch to new in Selennium for Pythonn

](https://stackoverflow.com/questions/10629815/how-to-switch-to-new-window-in-selenium-for-python)

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?