安装selenium gird

515 阅读1分钟

github.com/SeleniumHQ/…

1、centos安装docker compose

  142  [2021-06-24 13:00:05] yum install python3
  158  [2021-06-24 15:00:01] pip3 install docker-compose
  159  [2021-06-24 15:00:15] docker-compose version

2、docker-compose-v3.yml

# To execute this docker-compose yml file use `docker-compose -f docker-compose-v3.yml up`
# Add the `-d` flag at the end for detached execution
# To stop the execution, hit Ctrl+C, and then `docker-compose -f docker-compose-v3.yml down`
version: "3"
services:
  chrome:
    image: selenium/node-chrome:4.0.0-rc-1-prerelease-20210618
    volumes:
      - /dev/shm:/dev/shm
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
      - SE_NODE_MAX_SESSIONS=8 
    ports:
      - "6900:5900"

  edge:
    image: selenium/node-edge:4.0.0-rc-1-prerelease-20210618
    volumes:
      - /dev/shm:/dev/shm
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
      - SE_NODE_MAX_SESSIONS=8
    ports:
      - "6901:5900"

  firefox:
    image: selenium/node-firefox:4.0.0-rc-1-prerelease-20210618
    volumes:
      - /dev/shm:/dev/shm
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
      - SE_NODE_MAX_SESSIONS=8
    ports:
      - "6902:5900"

  selenium-hub:
    image: selenium/hub:4.0.0-rc-1-prerelease-20210618
    container_name: selenium-hub
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"

tips:blog.51cto.com/fei007/1166… (1)在vim中,进入命令模式输入:set paste,在进行粘贴,就不会乱码了。但是这样存在一个问题,就是不会自动产生缩进了,因此需要在粘贴完成之后命了输入:set nopaste,恢复缩进模式。

[root@10-89-104-53-kvm-thdx selenium-gird]# docker-compose -f docker-compose-v3.yml up -d
Creating selenium-hub ... done
Creating selenium-gird_edge_1    ... done
Creating selenium-gird_firefox_1 ... done
Creating selenium-gird_chrome_1  ... done
[root@10-89-104-53-kvm-thdx selenium-gird]# 

3、界面 xxxxx:4444

image.png

4、代码测试

# 单进程
from selenium import webdriver
from loguru import logger
from concurrent.futures import ProcessPoolExecutor

firefox_options = webdriver.FirefoxOptions()


def get_title(link):
    firefox_options = webdriver.FirefoxOptions()
    with webdriver.Remote(
            command_executor='http://10.89.104.53:4444/wd/hub',
            options=firefox_options
    ) as driver:
        driver.get(link)
        logger.info(driver.title)


# 单进程
for i in range(20):
    link = f"https://www.bing.com/search?q={i}&mkt=zh-CN"
    get_title(link)

# 多进程
# pool = ProcessPoolExecutor(8)
# for i in range(20):
#     link = f"https://www.bing.com/search?q={i}&mkt=zh-CN"
#     pool.submit(get_title, link)