python - 如何在一段时间没有事件后禁用 Selenium Chrome 驱动程序的自动关闭操作?

标签 python selenium selenium-webdriver selenium-chromedriver

我使用 selenium RemoteWebdriver 连接到远程集线器。

options = webdriver.ChromeOptions()
options.add_argument("window-size=1920,1080")
options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36")
driver = webdriver.Remote('http://127.0.0.1:4444/wh/hub', desired_capabilities=options.to_capabilities())
driver.get('https://example.com')

如果一段时间内没有任何事件, session 将自动关闭。

>>> driver.title
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 342, in title
    resp = self.execute(Command.GET_TITLE)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: No active session with ID 227c970ff0d633fce0e68c57c40e70b7

这是selenium/standalone-chrome-debug:3.141.59-mercury容器中的日志:

chrome_1_9da7c6574f3b | 04:27:09.199 INFO [ActiveSessionFactory.apply] - Capabilities are: {
chrome_1_9da7c6574f3b |   "browserName": "chrome",
chrome_1_9da7c6574f3b |   "goog:chromeOptions": {
chrome_1_9da7c6574f3b |     "extensions": [
chrome_1_9da7c6574f3b |     ],
chrome_1_9da7c6574f3b |     "args": [
chrome_1_9da7c6574f3b |       "window-size=1920,1080",
chrome_1_9da7c6574f3b |       "user-agent=Mozilla\u002f5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit\u002f537.36 (KHTML, like Gecko) Chrome\u002f73.0.3683.86 Safari\u002f537.36"
chrome_1_9da7c6574f3b |     ]
chrome_1_9da7c6574f3b |   },
chrome_1_9da7c6574f3b |   "version": ""
chrome_1_9da7c6574f3b | }
chrome_1_9da7c6574f3b | 04:27:09.200 INFO [ActiveSessionFactory.lambda$apply$11] - Matched factory org.openqa.selenium.grid.session.remote.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)
chrome_1_9da7c6574f3b | Starting ChromeDriver 73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72) on port 15565
chrome_1_9da7c6574f3b | Only local connections are allowed.
chrome_1_9da7c6574f3b | Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
chrome_1_9da7c6574f3b | 04:27:09.809 INFO [ProtocolHandshake.createSession] - Detected dialect: OSS
chrome_1_9da7c6574f3b | 04:27:09.822 INFO [RemoteSession$Factory.lambda$performHandshake$0] - Started new session 227c970ff0d633fce0e68c57c40e70b7 (org.openqa.selenium.chrome.ChromeDriverService)

----: No activities here:-----


chrome_1_9da7c6574f3b | 04:59:04.125 INFO [ActiveSessions$1.onStop] - Removing session 227c970ff0d633fce0e68c57c40e70b7 (org.openqa.selenium.chrome.ChromeDriverService)

所以我猜 chrome 驱动程序会在 30 分钟内自动关闭无事件 session 。 我想知道如何在没有事件的情况下禁用自动关闭操作?

最佳答案

我从code here找到了线索

-sessionTimeout

Specifies the timeout before the server automatically kills a session that hasn't had any activity in the last X seconds. The test slot will then be released for another test to use. This is typically used to take care of client crashes. For grid hub/node roles, cleanUpCycle must also be set. If a node does not specify it, the hub value will be used.

超时确实是1800s,与日志预期的相同。

然后我发现了另一个 question关于这个。

java -jar /opt/selenium/selenium-server-standalone.jar -sessionTimeout 31536000

我对java不熟悉。 我使用 selenium/standalone-chrome-debug:3.141.59-mercury docker 镜像来运行 chromdriver。 所以如果你也使用docker,可以在运行docker时设置env。

这是我的 docker-compose 文件:

chrome:
  image: selenium/standalone-chrome-debug:3.141.59-mercury
  volumes:
    - /dev/shm:/dev/shm
  ports:
    - 127.0.0.1:4444:4444
    - 127.0.0.1:5900:5900
  environment:
     - SE_OPTS=-sessionTimeout 31536000

超时时间约为一年。

关于python - 如何在一段时间没有事件后禁用 Selenium Chrome 驱动程序的自动关闭操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55468808/

相关文章:

c# - 你如何在 Selenium 中设置 ChromeDriver 的端口?

python - 在数据框的位置 [0] 处插入行

python - 强制发生错误时无法打印 except 语句

JavascriptExecutor SyntaxError : Unexpected identifier. 为什么?

java - 使用 Selelnium 时出现错误

java - 如何使用 HP UFT 或 Selenium Webriver 检索开放图形的元标记属性

python - 不使用 numpy 数组转置 h5py 数据集

python - 在 sqlalchemy 中重新排序复合主键

css - 如何为包含特定文本的元素编写 css?

java - 如何捕获工具提示及其文本