In Python, how to check if Selenium WebDriver has left or not?

The following is sample code:

from selenium import webdriver driver = webdriver.Firefox() 

(The window closes for some reason)

 driver.quit() 

Traceback (last last call): File ", line 1, to the file" / usr / local / lib / python 2.7 / dist-packages / selenium / webdriver / firefox / webdriver.py ", line 183, in the brochure File RemoteWebDriver.quit (self) "/ usr / local / lib / python 2.7 / dist-packages / selenium / webdriver / remote / webdriver.py", line 592, in the brochure self.execute (Command.QUIT) File "/ usr / local / lib / python 2.7 / dist-packages / selenium / webdriver / remote / webdriver.py ", line 297, executed by self.error_handler.check_response (response) File" / usr / local / lib / python 2.7 / dist-packages / selenium / webdriver / remote / errorhandler.py ", line 194, at check_response raise exception_class (message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: I tried to run the command without establishing a connection

Is there a way to check if a webdriver instance is active?

0
source share
1 answer

You can use something like this that psutil uses

 from selenium import webdriver import psutil driver = webdriver.Firefox() driver.get("http://tarunlalwani.com") driver_process = psutil.Process(driver.service.process.pid) if driver_process.is_running(): print ("driver is running") firefox_process = driver_process.children() if firefox_process: firefox_process = firefox_process[0] if firefox_process.is_running(): print("Firefox is still running, we can quit") driver.quit() else: print("Firefox is dead, can't quit. Let kill the driver") firefox_process.kill() else: print("driver has died") 
+2
source

Source: https://habr.com/ru/post/1275257/


All Articles