I started a Docker container with:
docker run -d --shm-size="4g" --hostname selenium_firefox selenium/standalone-firefox
In another container with Python:
...
>>> driver = webdriver.Remote(command_executor="http://" selenium_host ":4444/w
d/hub", desired_capabilities=DesiredCapabilities.FIREFOX, keep_alive=True)
>>> driver.title
''
>>> driver.title
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/webdri
ver.py", line 447, in title
resp = self.execute(Command.GET_TITLE)
File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/webdri
ver.py", line 424, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/errorh
andler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Unable to execute reques
t for an existing session: Unable to find session with ID: 5c619451-8361-4ec9-9b
7e-58b7afac15ff
Build info: version: '4.1.1', revision: 'e8fcc2cecf'
System info: host: 'selenium_firefox', ip: '172.17.0.3', os.name: 'Linux', os.ar
ch: 'amd64', os.version: '5.4.0-89-generic', java.version: '11.0.13'
Driver info: driver.version: unknown
The first driver.title I ran it immediately after creating the remote webdriver.
Then I waited for some time (around 15 minutes) and ran driver.title again, and it seem that the Python console has lost connection to the corresponding browser.
Why does this happen and how do I avoid it? It doesn't happen if I don't use a remote webdriver.
CodePudding user response:
Option 1: Override Docker Selenium Grid default session timeout
From docker/selenium documentation:
Grid has a default session timeout of 300 seconds, where the session can be on a stale state until it is killed. You can use SE_NODE_SESSION_TIMEOUT to overwrite that value in seconds.
docker run -d -e SE_NODE_SESSION_TIMEOUT=1000 --shm-size="4g" --hostname selenium_firefox selenium/standalone-firefox
Option 2: Ping your session once in 60 (any < 300) seconds
You may execute some driver command in a loop during the idle time
for x in range(15):
time.sleep(60)
driver.current_url
Reference
https://github.com/SeleniumHQ/docker-selenium#grid-url-and-session-timeout
