Selenium python не может создать сессию два раза подряд. Я закрываю сессию после первого раза (см. Скрипт python ниже)
docker -compose config
version: '3.6'
services:
hub:
image: selenium/hub:3.141.59
ports:
- 4444:4444
environment:
- GRID_DEBUG=true
networks:
- selenium
chrome:
image: selenium/node-chrome-debug:3.141.59
restart: always
depends_on:
- hub
ports:
# VNC port. Password: secret
- 5901:5900
environment:
- HUB_HOST=hub
- HUB_PORT_PARAM=4444
- GRID_DEBUG=true
networks:
- selenium
# https://github.com/SeleniumHQ/docker-selenium#running-the-images
shm_size: 3G
python скрипт для проверки
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
HUB = 'http://127.0.0.1:4444/wd/hub'
URL = 'https://google.com'
if __name__ == '__main__':
print('before create')
driver = webdriver.Remote(HUB, {
**DesiredCapabilities.CHROME,
'loggingPrefs': {'driver': 'DEBUG', 'browser': 'INFO'},
})
print('after create')
driver.get(URL)
print(driver.current_url)
driver.close()
Launch
$ docker-compose up -d
$ python web.py
before create
after create
$ python web.py
before create
... и python висит там.
Launch 2 Вот тот же вывод, но к нему добавлены некоторые запросы и ответы от python selenium webdriver.
$ docker-compose stop && docker-compose up -d
$ python web.py
before create
-- params POST http://127.0.0.1:4444/wd/hub/session {"capabilities": {"firstMatch": [{}], "alwaysMatch": {"browserName": "chrome", "platformName": "any"}}, "desiredCapabilities": {"browserName": "chrome", "version": "", "platform": "ANY", "loggingPrefs": {"driver": "DEBUG", "browser": "INFO"}}} {'Accept': 'application/json', 'Content-Type': 'application/json;charset=UTF-8', 'User-Agent': 'selenium/3.141.0 (python linux)'}
executed response {'value': {'sessionId': 'e98e8702b439059644f0c29b33c1f983', 'capabilities': {'acceptInsecureCerts': False, 'browserName': 'chrome', 'browserVersion': '79.0.3945.117', 'chrome': {'chromedriverVersion': '79.0.3945.36 (3582db32b33893869b8c1339e8f4d9ed1816f143-refs/branch-heads/3945@{#614})', 'userDataDir': '/tmp/.com.google.Chrome.JFZwML'}, 'goog:chromeOptions': {'debuggerAddress': 'localhost:42381'}, 'networkConnectionEnabled': False, 'pageLoadStrategy': 'normal', 'platformName': 'linux', 'proxy': {}, 'setWindowRect': True, 'strictFileInteractability': False, 'timeouts': {'implicit': 0, 'pageLoad': 300000, 'script': 30000}, 'unhandledPromptBehavior': 'dismiss and notify', 'webdriver.remote.sessionid': 'e98e8702b439059644f0c29b33c1f983'}}}
after create
-- params POST http://127.0.0.1:4444/wd/hub/session/e98e8702b439059644f0c29b33c1f983/url {"url": "https://google.com"} {'Accept': 'application/json', 'Content-Type': 'application/json;charset=UTF-8', 'User-Agent': 'selenium/3.141.0 (python linux)'}
executed response {'value': None}
-- params GET http://127.0.0.1:4444/wd/hub/session/e98e8702b439059644f0c29b33c1f983/url None {'Accept': 'application/json', 'Content-Type': 'application/json;charset=UTF-8', 'User-Agent': 'selenium/3.141.0 (python linux)'}
executed response {'value': 'https://www.google.com/'}
https://www.google.com/
-- params DELETE http://127.0.0.1:4444/wd/hub/session/e98e8702b439059644f0c29b33c1f983/window None {'Accept': 'application/json', 'Content-Type': 'application/json;charset=UTF-8', 'User-Agent': 'selenium/3.141.0 (python linux)'}
executed response {'value': None}
$ python web.py
before create
before execution
-- params POST http://127.0.0.1:4444/wd/hub/session {"capabilities": {"firstMatch": [{}], "alwaysMatch": {"browserName": "chrome", "platformName": "any"}}, "desiredCapabilities": {"browserName": "chrome", "version": "", "platform": "ANY", "loggingPrefs": {"driver": "DEBUG", "browser": "INFO"}}} {'Accept': 'application/json', 'Content-Type': 'application/json;charset=UTF-8', 'User-Agent': 'selenium/3.141.0 (python linux)'}
Python версия: 3.6.8 docker изображения свободны sh как сейчас