0
0

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

結論

事前にインストール

  • pip install selenium webdriver-manager

プログラム

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from time import sleep

    
chrome_options = Options()

# デバッグモードで開く
chrome_options.add_argument("--remote-debugging-port=9222")

# detach=True スクリプトが終了してもブラウザを閉じないようにします
chrome_options.add_experimental_option("detach", True) 

# 最新のドライバーをインストール
chrome_service = Service(ChromeDriverManager().install())

driver = webdriver.Chrome(service=chrome_service, options=chrome_options)

driver.get("http://yahoo.co.jp")

もう一度開きたい場合

chrome_options = Options()

# 重要なのはこの部分
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")

driver = webdriver.Chrome(options=chrome_options)

# 確認
print(f"現在のURL: {driver.current_url}")
print(f"タイトル: {driver.title}")
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?