I want to check if a browser opened with Selenium is still open. I would need this check for closing a program. If the browser is not open, then the GUI should be destroyed without a message (tk.messagebox) coming. But if the browser is open, then the message should come as soon as the function is activated.
Here is the function:
JavaScript
x
16
16
1
def close():
2
msg_box = tk.messagebox.askquestion('Exit Application', 'Are you sure you want to exit the application?',
3
icon='warning')
4
if msg_box == 'yes':
5
root.destroy()
6
try:
7
web.close()
8
except NameError:
9
sys.exit(1)
10
except InvalidSessionIdException:
11
sys.exit(1)
12
except WebDriverException:
13
sys.exit(1)
14
else:
15
return
16
Advertisement
Answer
I don’t think there is a direct api for checking browser status. But you can use the the work around
JavaScript
1
8
1
def isBrowserAlive(driver):
2
try:
3
driver.current_url
4
# or driver.title
5
return True
6
except:
7
return False
8