Building selenium distributed environment based on docker

  • 2021-09-20 21:57:01
  • OfStack

1. Download the image

docker pull selenium/hub
docker pull selenium/node-firefox
docker pull selenium/node-chrome

Note: selenium/node-firefox and selenium/node-chrome are interface-free.

To see the real-time running interface, you need to use one of the following two images.

docker pull selenium/standalone-chrome-debug
docker pull selenium/standalone-firefox-debug

2. Start Docker of Hub node

docker run -p 4444:4444 -d --name hub selenium/hub

Parameter description:

run: Run a mirror and create a container. -p 4444: 4444: Maps ports in container. -d: Running in the background --name: The name of the container, which I will call hub directly

3. Start Docker of Node node

docker run -P -d --link hub:hub --name firefox selenium/node-firefox
docker run -P -d --link hub:hub --name chrome selenium/node-chrome

Or Node node with Debug interface

docker run -d -p 5900:5900 --link hub:hub selenium/node-chrome-debug

Parameter description:

--link is a container with the link alias hub.

4. Install and configure VNC

VNC (Virtual Network Console) is the abbreviation of Virtual Network Console. Is an excellent remote control tool software, based on UNIX and Linux operating system free open source software, remote control capability is powerful, efficient and practical.

Download address: https://www.realvnc.com/en/connect/download/viewer/

5. Test the code

Using the Selenium Grid service requires connecting to the service using the webdriver. Remote method and passing in the desired capabilities of desired_capbilities. The sample script is as follows.


from time import sleep
from selenium import webdriver


driver = webdriver.Remote(
command_executor='http://192.168.99.100:4444/wd/hub',
desired_capabilities={'browserName': 'chrome'}
)

driver.get('https://www.baidu.com')
print("start run")
sleep(1)
print(driver.title)
driver.quit()
print("end...")

Related articles: