-
Notifications
You must be signed in to change notification settings - Fork 1.4k
How to handle an unexpectedly closed browser? #3475
-
Hi,
I have the following code:
with SB(uc=True, test=True, locale_code="en") as sb:
sb.activate_cdp_mode()
print("waiting")
input()
print("go!")
# and .... it hangs, no exception, no message, nothing :(
sb.open('http://google.com')
When the script is waiting for my input, I manually close the browser and then press Enter
. The script resumes but hangs indefinitely, waiting for... something?
How can I handle this situation? I'd like to detect that the browser was closed unexpectedly and restart it if needed.
Best regards!
Beta Was this translation helpful? Give feedback.
All reactions
The browser wouldn't close unexpectedly in CDP Mode unless you manually closed it.
If you wanted to close the browser, you would first call sb.reconnect()
and then sb.driver.quit()
.
But it's probably better to wait until the end of the with
block for the automatic browser quit / close.
Also, you may want to use a Python breakpoint()
instead of input()
if you're just trying to pause the script.
Replies: 1 comment 3 replies
-
The browser wouldn't close unexpectedly in CDP Mode unless you manually closed it.
If you wanted to close the browser, you would first call sb.reconnect()
and then sb.driver.quit()
.
But it's probably better to wait until the end of the with
block for the automatic browser quit / close.
Also, you may want to use a Python breakpoint()
instead of input()
if you're just trying to pause the script.
Beta Was this translation helpful? Give feedback.
All reactions
-
Thank you so much for the quick response Michael! Wow! :)
Now, let me get into the details.
Let's say I have a crontab script that runs every hour and terminates all browser instances. I've noticed that if the browser process is killed while the script is running, it also causes it to hang. The input()
was just there to clearly demonstrate the issue.
What I would really like to achieve is: if something closes the browser, SeleniumBase detects it and automatically restarts the browser and the script.
If SeleniumBase threw an exception, triggered an event, or provided any kind of signal, I could react accordingly. However, right now, the script execution just freezes, and I can't do anything about it. I assume I might be doing something wrong.
with SB(uc=True, test=True, locale_code="en") as sb:
sb.activate_cdp_mode()
sb.open('http://google.com')
sb.sleep(5) # browser goes down
# and the script hangs
sb.open('http://apple.com')
Do you have any recommendations on how to handle this?
Thanks again! 😊
Beta Was this translation helpful? Give feedback.
All reactions
-
If your script is automatically terminating browser instances, then maybe also have it terminate the python
command that is running the program. If there's any "hang", there would be a timeout from the SeleniumBase side if something goes wrong. But if it's hanging somewhere else (eg. in the middle of the CDP call), then SeleniumBase would have to wait until CDP gives back control of the program. Might not happen depending on how it was terminated.
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 1
-
Ok, thanks! I understand now. So it looks like the script might end up waiting indefinitely for a response from the browser, which will never come... because the browser is already gone.
Yes, I definitely should terminate the Python command that is running the program, as you suggested. However, I'm also thinking about scenarios where the browser might unexpectedly close due to an error or other unforeseen issues. I'm considering different exceptions and failure cases that could happen.
Thanks again for your response! I'll experiment a bit more and see what I can come up with. :)
Beta Was this translation helpful? Give feedback.