前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Chrome又又又自动更新了,是时候自动下载driver了

Chrome又又又自动更新了,是时候自动下载driver了

作者头像
zx钟
发布2022-12-02 15:36:03
7060
发布2022-12-02 15:36:03
举报
文章被收录于专栏:测试游记测试游记测试游记

在进行UI自动化的时候,需要下载对应的driver来控制浏览器,下面参考seleniumbase实现一个下载指定版本chromedriver

查看seleniumbase中下载chromedriver的操作

seleniumbase.console_scripts.main中实现了driver文件的下载

它的用法为

sbase get chromedriver 
sbase get chromedriver 101.0.4951.41
sbase get chromedriver latest-1
sbase get edgedriver 101.0.1210.32

步骤为:

  1. 获取当前操作系统
  2. 根据操作系统获取要下载的driver文件
  3. 根据文件名+版本信息拼接下载路径
  4. 下载到指定位置并解压

获取当前操作系统

使用platform模块可以获取到当前操作系统

方法为:platform.system()

根据操作系统确定指定driver名称

下载的位置TOOL_PATH可根据实际情况修改

import os
import platform

TOOL_PATH = "/Users/zhongxin/gitproject/wytest/src/tools"

class DriverOperator:
    def __init__(self):
        self.system = platform.system()
        if self.system == "Windows":
            self.path = f'{TOOL_PATH}/driver/win'
            self.chrome_file_name = "chromedriver_win32.zip"
        elif self.system == "Linux":
            self.path = f'{TOOL_PATH}/driver/linux'
            self.chrome_file_name = "chromedriver_linux64.zip"
        elif self.system == "Darwin":
            self.path = f'{TOOL_PATH}/driver/mac'
            self.chrome_file_name = "chromedriver_mac64.zip"
        else:
            raise Exception("未适配当前操作系统")

获取最新版本Chromedriver

  1. 访问`https://chromedriver.storage.googleapis.com/LATEST_RELEASE`可以拿到当前最新的driver对象版本
  2. 访问`https://chromedriver.storage.googleapis.com/LATEST_RELEASE_101`可以拿到当前大版本101下的最新的driver对象版本

拼接获取版本地址

last = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE"
version = version or requests.get(last).text
if len(version) <  and version.isdigit() and int(version) > :
    version = requests.get(f"{last}_{version}").text

下载并解压

在国内镜像处下载Chromedriver可以提升速度

download_url = f"https://registry.npmmirror.com/-/binary/chromedriver/{version}/{self.chrome_file_name}"
remote_file = requests.get(download_url)
file_path = os.path.join(self.path, self.chrome_file_name)
with open(file_path, "wb") as file:
    file.write(remote_file.content)

删除路径下旧的driver,解压新driver到指定文件

zip_ref = zipfile.ZipFile(file_path, "r")
contents = zip_ref.namelist()
new_file = ""
if len(contents) == :
    f_name = contents[]
    new_file = os.path.join(self.path, str(f_name))
    if os.path.exists(new_file):
        os.remove(new_file)
    zip_ref.extractall(self.path)
    zip_ref.close()
    os.remove(file_path)
    os.system(f"chmod -R 777 {new_file}")
return new_file

完整代码

# -*- coding: utf-8 -*-
# @Time    : 2022/7/11 17:44
# @Author  : zhongxin
# @Email   : 490336534@qq.com
# @File    : driveroperator.py
import os
import platform
import zipfile

import requests

from src.utils.constant import TOOL_PATH


class DriverOperator:
    def __init__(self):
        self.system = platform.system()
        if self.system == "Windows":
            self.path = f'{TOOL_PATH}/driver/win'
            self.chrome_file_name = "chromedriver_win32.zip"
        elif self.system == "Linux":
            self.path = f'{TOOL_PATH}/driver/linux'
            self.chrome_file_name = "chromedriver_linux64.zip"
        elif self.system == "Darwin":
            self.path = f'{TOOL_PATH}/driver/mac'
            self.chrome_file_name = "chromedriver_mac64.zip"
        else:
            raise Exception("未适配当前操作系统")

    def chrome_driver(self, version=None):
        """
        下载ChromeDriver
        @param version: 完整版本或者大版本号,不传入则下载最新版本
            例如:
                * 103.0.5060.53
                * 103
        @return:
        """
        last = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE"
        version = version or requests.get(last).text
        if len(version) <  and version.isdigit() and int(version) > :
            version = requests.get(f"{last}_{version}").text
        download_url = f"https://registry.npmmirror.com/-/binary/chromedriver/{version}/{self.chrome_file_name}"
        remote_file = requests.get(download_url)
        file_path = os.path.join(self.path, self.chrome_file_name)
        with open(file_path, "wb") as file:
            file.write(remote_file.content)
        zip_ref = zipfile.ZipFile(file_path, "r")
        contents = zip_ref.namelist()
        new_file = ""
        if len(contents) == :
            f_name = contents[]
            new_file = os.path.join(self.path, str(f_name))
            if os.path.exists(new_file):
                os.remove(new_file)
            zip_ref.extractall(self.path)
            zip_ref.close()
            os.remove(file_path)
            os.system(f"chmod -R 777 {new_file}")
        return new_file

测试使用

d = DriverOperator()
new_file = d.chrome_driver()
from selenium import webdriver

driver = webdriver.Chrome(executable_path=new_file)
print(driver.capabilities["browserVersion"])  # 当前浏览器版本
print(driver.capabilities["chrome"]["chromedriverVersion"])  # 当前driver版本
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-07-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 测试游记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 查看seleniumbase中下载chromedriver的操作
  • 获取当前操作系统
  • 根据操作系统确定指定driver名称
  • 获取最新版本Chromedriver
  • 下载并解压
  • 完整代码
  • 测试使用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档