0%

docker之docker-compose安装selenium

  • 在服务器上安装docker-compose
1
pip3 install docker-compose
  • 把本地windows10中的中文字体(C:\Windows\Fonts)随便选择几种拷贝到服务器的路径(/usr/local/selenium_grid/fonts

image-20220324193321079

  • 查看字体
1
2
3
4
5
6
7
8
/usr/local/selenium_grid/fonts
[root@VM-24-13-centos fonts]# ll
总用量 65268
-rw-r--r-- 1 root root 16829116 3月 24 18:15 msyhbd.ttc
-rw-r--r-- 1 root root 12139380 3月 24 18:15 msyhl.ttc
-rw-r--r-- 1 root root 19647736 3月 24 18:14 msyh.ttc
-rw-r--r-- 1 root root 18214472 3月 24 18:15 simsun.ttc

  • 编写compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
chrome: 
image: selenium/node-chrome:3.8.1
links:
- hub:hub # 这里是把挂载到hub下面的意思
ports:
- "5902:5900" # 给vnc调试用
environment:
- NODE_MAX_INSTANCES=5 # 实例化和下面参数一般保持一致,可以多机并行
- NODE_MAX_SESSION=5
- SCREEN_WIDTH=1920
- SCREEN_HEIGHT=1080
volumes:
- /dev/shm:/dev/shm # 挂载这个持久化数据,据说是为了防止不同的闪退
- ./fonts:/usr/share/fonts # 把中文字体挂载进来,解决截图乱码问题

hub:
image: selenium/hub:3.8.1
ports:
- "7777:4444" # 7777为外部web访问端口
  • 执行compose,-d表示后台执行
1
docker-compose up -d
  • 查看下镜像和容器

image-20220323192225606

image-20220323192256161

  • 远程查看下,输入:http://ip:7777/grid/console/?config=true&configDebug=true

image-20220323192506464

  • 编写测试代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 pip3 install selenium

import time

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from multiprocessing import Process

print(11111)
# ip = "远程ip"
ip = "localhost"
driver = webdriver.Remote(
command_executor="http://%s:7777/wd/hub" %ip,
desired_capabilities=DesiredCapabilities.CHROME
)
print(22222)

def test(i):
time.sleep(3)
driver.get("https://www.baidu.com")
print(driver.title, i)


if __name__ == '__main__':
test(1)
driver.close()
driver.quit()
  • 无论是本地还是服务器运行测试代码已经通过

image-20220323192620673