python - Selenium:闲置几分钟后为 "Unable to find session with ID"

标签 python selenium

我启动了一个 Docker 容器: docker run -d --shm-size="4g"--hostname selenium_firefox selenium/standalone-firefox

在另一个装有 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

第一个 driver.title 我在创建远程 webdriver 后立即运行它。

然后我等了一段时间(大约15分钟),再次运行driver.title,Python控制台似乎已经失去了与相应浏览器的连接。

为什么会发生这种情况,我该如何避免?如果我不使用远程网络驱动程序,则不会发生这种情况。

最佳答案

选项 1:覆盖 Docker Selenium Grid 默认 session 超时

来自 docker/selenium 文档:

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

选项 2:每 60(任何 < 300)秒 Ping 一次您的 session

你可以在空闲时间循环执行一些驱动命令

for x in range(15):
    time.sleep(60)
    driver.current_url

引用

https://github.com/SeleniumHQ/docker-selenium#grid-url-and-session-timeout

关于python - Selenium:闲置几分钟后为 "Unable to find session with ID",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70794535/

相关文章:

python - Matplotlib注释:(文本框)与图形标题重叠

google-chrome - selenium.common.exceptions.WebDriverException : Message: Service

Java + Selenium : ElementNotVisibleException

java - 如何为字段值选择 "Colour"以及字段的滚动参数值?

python - 简单功能不起作用,看不到错误

java - 使用getRuntime()。exec()执行脚本时执行错误处理的最佳方法

python - Selenium 网络驱动程序 : form data not present after submit

Python:请求 session 登录 Cookie

python - 如何在网站上运行 python 脚本?

python - body 印 Selenium