How to check if chromedriver exist or running?
Asked Answered
P

1

6

I am using 3rd party software to create a fancy application GUI with several buttons.

Each buttons will execute different .py file/.exe file. For instance:-

btnYahoo = execute yahoo.py/yahoo.exe

btnGoogle = execute google.py/google.exe

So inside both py scripts is using chromedriver to launch Chrome Browser and redirect to specific URLs

google.py

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(executable_path=r'chromedriver.exe')
driver.get("https://www.google.com")

yahoo.py

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(executable_path=r'chromedriver.exe')
driver.get("https://malaysia.yahoo.com/?p=us")

So if I execute both scripts above, it will launch 2 chrome browser.

Therefore, my concern is how can I check is webdriver.Chrome is running?

If so, then assign the webdriver.Chrome to a variable, so that I can open new tab and do further automate script progress.

For example of expected result:

  1. Execute google.py - A new chrome browser is open and redirect to www.google.com

  2. Execute yahoo.py - If webdriver.Chrome is executed/existed, then assign the browser to driver variable. Else launch new browser

Thanks for advance information.

Partnership answered 13/2, 2019 at 17:29 Comment(4)
You should probably call driver.quit(). If you do not use quit() at the end of program, the WebDriver session will not be closed properly and the files will not be cleared off memory. This may result in memory leak errors. This could be looked up in the task manager: it will have chromedriver.exe remaining from previous runs. This happens in the JavaScript version of the selenium-webdriver client and might also be the case for Python.Stupendous
@cnishina I don't wanna close the webdriver tho. Instead, I just want to check if webdriver is exist. If so, then assign the webdriver to a variable, else launch new browser.Partnership
You shouldn't need to use Selenium just to launch a browser with a URL. Check how to do this for the OS you are using. It should be as simple as opening a provided URL... the default browser should be used and navigate to that URL.Awlwort
@Awlwort Please kindly read my post properly. I had mentioned "do further automate script progress". Which mean I use Selenium is not just only for redirecting. ThanksPartnership
B
1

I was able to do so by checking if driver.session_id was None.

Brendon answered 11/3, 2022 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.