selenium grid分布式

selenium grid环境在很多实体电脑上可以实现,如果我们没有那么多电脑,就需要利用docker来完成selenium grid环境搭建

一、环境搭建

  1. 创建一个docker网络

    复制代码
    docker network create grid
  2. 完成中心的搭建

  • 拉取镜像

    复制代码
    docker pull selenium/hub:4.10
  • 启动镜像

    复制代码
    docker run -d -p 4442-4444:4442-4444 --network grid --name shub -v
    /etc/localtime:/etc/localtime:ro --privileged=true selenium/hub:4.10
  • 启动成功后,浏览器访问(http://ip:4444/ui)

3.完成chrome节点搭建

  • 拉取镜像

    复制代码
    docker pull selenium/node-chrome:4.10
  • 启动镜像

    复制代码
    docker run -d -p 7900:7900 --network grid -e SE_EVENT_BUS_HOST=shub --
    shm-size="2g" -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e
    SE_EVENT_BUS_SUBSCRIBE_PORT=4443 --name chrome -v
    /etc/localtime:/etc/localtime:ro --privileged=true selenium/nodechrome:4.10
  • 可以通过下面的链接打开docker容器浏览器可视化界面

    复制代码
    http://ip:7900/?autoconnect=1&resize=scale&password=secret

4.完成selenium gird的框架修改

python 复制代码
class DriverOperate:
    # 利用类属性定一个全局核心操作对象,他的赋值未来必须在conftest里完成
    globalDriverOperate=None
    # 这个类将会封装我们所有的核心操作
    # 在之后我们的这个框架,将会调用该类提供的各种动作来实现操作
    def __init__(self,browser='chrome',remote_url=None):
        self.logger = GetLogger.get_logger()
        self.browser = browser.lower()
        if remote_url:
            # 要用selenium grid方式运行
            if self.browser == 'chrome':
                options = webdriver.ChromeOptions()

            elif self.browser == 'firefox':
                options = webdriver.FirefoxOptions()

            elif self.browser == 'ie':
                options = webdriver.IeOptions()

            elif self.browser == 'edge':
                options = webdriver.EdgeOptions()

            elif self.browser == 'safari':
                options = webdriver.safari.options.Options()

            else:
                self.logger.error(f'{self.browser} 不支持')
                raise BaseException(f'{self.browser} 不支持')
            options.page_load_strategy='eager' # 表示页面加载较快
            self.driver = webdriver.Remote(remote_url,options=options)
        else:
            # 执行本地浏览器
            if self.browser == 'chrome':
                options = webdriver.ChromeOptions()
                self.driver = webdriver.Chrome(options=options)
            elif self.browser == 'firefox':
                options = webdriver.FirefoxOptions()
                self.driver = webdriver.Firefox(options=options)
            elif self.browser == 'ie':
                options = webdriver.IeOptions()
                self.driver = webdriver.Ie(options=options)
            elif self.browser == 'edge':
                options = webdriver.EdgeOptions()
                self.driver = webdriver.Edge(options=options)
            elif self.browser == 'safari':
                options = webdriver.safari.options.Options()
                self.driver = webdriver.Safari(options=options)
            else:
                self.logger.error(f'{self.browser} 不支持')
                raise BaseException(f'{self.browser} 不支持')
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)
        self.logger.info(f'{self.browser} 启动成功')
相关推荐
MYians1 天前
[Windows]Postman-app官方历史版本下载方法
测试工具·postman
巴里巴气2 天前
selenium基础知识 和 模拟登录selenium版本
爬虫·python·selenium·爬虫模拟登录
2501_924064112 天前
2025年跨端云真机测试平台深度测评:XR与折叠屏时代的兼容性之战
测试工具·移动端自动化测试·自动化测试脚本
Small black human2 天前
HTTP-Postman的安装及其使用
测试工具·postman
AIZHINAN2 天前
Appium 简介
自动化测试·测试工具·appium
吴free2 天前
mac电脑wireshark快速实现http接口抓包
网络·测试工具·http·wireshark
DeamoTech3 天前
ESCADA
物联网·测试工具
旷世奇才李先生3 天前
Selenium 安装使用教程
selenium·测试工具
巴里巴气4 天前
对selenium进行浏览器和驱动进行配置Windows | Linux
selenium·测试工具
q567315234 天前
Java Selenium反爬虫技术方案
java·爬虫·selenium