2
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?

Seleniumで指定したタブのみ閉じる

Posted at

事案

本題の通りの実装をしたくて
ネット調べたら、コードを書いてくれてたHPもあったが上手くいかなかったので、備忘録

解決

pip install selenium
from selenium import webdriver

driver = webdriver.Chrome()
# 一つ目のタブ
driver.get(url _1)

# 二つ目のタブ
driver.switch_to.new_window()
driver.get(url_2)

# 二つ目のタブのウィンドウハンドル取得
current_window = driver.current_window_handle

# driverのウィンドウハンドル取得
window_handles = driver.window_handles

# driverのウィンドウハンドルの中から二つ目のタブのウィンドウハンドルに一致するものを閉じる
for window in window_handles:
    if window == current_window:
        driver.close()
        
# 一つ目のタブのタブに切り替え 
driver.switch_to.window(window_handles[0])
2
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
2
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?