-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Description
What happened?
Hi,
I have checked all the related threads and documentation regarding the InvalidSessionIdException, tried different initializations, but still can't determine the root cause.
My environment:
- Apple M1 Pro, macOS Sequoia v15.2
- Python 3.11.9
- Selenium 4.27.1
- Safari 18.2 (Developer settings active, Allow remote automation enabled)
- safaridriver included with Safari 18.2 (20620.1.16.11.8)
So, I have this code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.safari.service import Service
def getData():
browser = webdriver.Safari(service=Service(executable_path="/usr/bin/safaridriver")) # tried webdriver.Safari() as well
time.sleep(5)
# load cookies
with open("cookies.json", "r") as f:
cookies = json.load(fp=f)
for cookie in cookies:
browser.add_cookie(cookie_dict=cookie) # <- HERE the exception is thrown
time.sleep(5)
browser.get("https://www.example-page-with-authentication.com")
# parsing data...
browser.quit()Whenever I run the script after some longer time (a few hours) or have the Safari quit (right-click on Safari in Dock -> select Quit) the below exception is thrown:
Traceback (most recent call last):
...
browser.add_cookie(cookie_dict=cookie)
File "/opt/homebrew/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 671, in add_cookie
self.execute(Command.ADD_COOKIE, {"cookie": cookie_dict})
File "/opt/homebrew/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 384, in execute
self.error_handler.check_response(response)
File "/opt/homebrew/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 232, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSessionIdException: Message:
However, once I run it immediately the 2nd time (or not quit Safari process manually), everything works fine. The cookies.json file contains cookies previously dumped using the selenium in Python CLI after the authentication (browser.get_cookies()).
I have also executed the below two lines as suggested in the other thread #5109
safaridriver -p 0
safaridriver --enablebut it didn't help. The first command seems to hang, the second is executed well.
Did I miss anything or do you have any suggestions what shall I check next?
Thanks for your time and insights!
How can we reproduce the issue?
Use the above code on the mentioned environment or clone the MRE provided in this repo: https://github.com/mariuszmatusiak/Safari_Webriver_4_Selenium_Issue_MRE
Relevant log output
Traceback (most recent call last):
File "/Users/mariusz/Projects/Safari_Webriver_4_Selenium_Issue_MRE/mre.py", line 35, in <module>
main()
File "/Users/mariusz/Projects/Safari_Webriver_4_Selenium_Issue_MRE/mre.py", line 32, in main
getData()
File "/Users/mariusz/Projects/Safari_Webriver_4_Selenium_Issue_MRE/mre.py", line 22, in getData
browser.add_cookie(cookie_dict=cookie)
File "/opt/homebrew/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 671, in add_cookie
self.execute(Command.ADD_COOKIE, {"cookie": cookie_dict})
File "/opt/homebrew/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 384, in execute
self.error_handler.check_response(response)
File "/opt/homebrew/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 232, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSessionIdException: Message:
Operating System
macOS Sequoia v15.2 and macOS Sonoma v14.7.2
Selenium version
Python 3.11.9 (from brew)
What are the browser(s) and version(s) where you see this issue?
Safari 18.2 (20620.1.16.11.8)
What are the browser driver(s) and version(s) where you see this issue?
/usr/bin/safaridriver included with Safari 18.2 (20620.1.16.11.8)
Are you using Selenium Grid?
No response