Selenium WebDriverの稼働チェックをするお話


SeleniumのWebDriver が 今動いているかどうかチェックするいい感じのメソッドがどうもないようなので、上手い方法ないかを検討。

とりあえず implicitly_wait(0) を応用してみる

from selenium import webdriver

def __init__(self, executable_path):
    self.executable_path = executable_path
    self.launch_driver()

def healthcheck_browser(self):
    try:
        # webdriverにWaitをいれる処理。デフォルト値0を入力し、
        # エラーが発生しないかをチェック
        self.driver.implicitly_wait(0)
    except Exception:
        # すでに quit されていれば エラー になるので再起動をかける
        print("The webdriver is not working.")
        launch_driver()
    else:
        print("The webdriver is now working.")

def launch_driver(self):
    self.driver = webdriver.Chrome(executable_path=self.executable_path)