【web自动化】selenium根据浏览器版本下载对应的chromedriver

1、使用国内的镜像地址

https://registry.npmmirror.com/binary.html?path=chromedriver/

 

2、通过simulation模拟用户点击来下载(只贴出部分方法)

#!/usr/bin/env python  
# -*- coding:utf-8 -*-  
import os
import platform
import signal
import time
import allure
import requests

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


DRIVER_DOWNLOAD_URL = "https://registry.npmmirror.com/binary.html?path=chromedriver/"


class BaseAction(object):
    def __init__(self, mode=0):
        """
        :param mode: 0: 表示无头模式
        """
        self.mode = mode

        self.options = webdriver.ChromeOptions()

        self.serv = Service(executable_path=r"{}".format(self.get_driver_path_by_sys()))
        self.serv.start()
        self.options.add_argument('--headless')
        self.options.add_argument('--disable-gpu')
        if self.sys_type == 'Windows':
            self.browser = webdriver.Chrome(service=self.serv, options=self.options)
        else:
            self.browser = webdriver.Chrome(executable_path=self.get_driver_path_by_sys(), options=self.options)

        self.browser_version = self.get_browser_ver()

    def get_driver_path_by_sys(self):
        self.sys_type = platform.system()
        if self.sys_type == "Windows":
            debug_logger.info("[web] system type: Windows")
            return os.path.join(PROJECT_ROOT_PATH, 'third_party', 'chromedriver', 'chromedriver_win32',
                                'chromedriver.exe')
        elif self.sys_type == "Linux":
            debug_logger.info("[web] system type: Linux")
            return os.path.join(PROJECT_ROOT_PATH, 'third_party', 'chromedriver', 'chromedriver_linux64',
                                'chromedriver')
        else:
            raise Exception("[web]system not support!")

    def get_browser_ver(self):
        self.open_url("http://www.baidu.com/")
        time.sleep(1)
        logger.info("[web]浏览器version:{}".format(self.browser.capabilities.get("browserVersion")))
        return self.browser.capabilities.get("browserVersion")

    def open_url(self, url):
        self.browser.get(url)
        time.sleep(2)
        self.wait = WebDriverWait(self.browser, 20)
        self.browser.maximize_window()def close(self):
        time.sleep(2)
        self.browser.close()
        self.browser.quit()
        self.serv.stop()

    def get_driver_uri(self):
        # 获取webdriver下载列表
        self.open_url(DRIVER_DOWNLOAD_URL)
        # 下载并更新当前webdriver列表
        ele = self.browser.find_elements(By.XPATH, '/html/body/table/tbody/tr/td/a')

        like_version_list = []
        for e in ele:
            if e.text.startswith(self.browser_version.rsplit(".", 1)[0]):
                like_version_list.append(e.text)
        logger.info(f'[web] like_version_list: {like_version_list}')
        # 获取最新版本
        logger.info(f'[web] version_uri: {max(like_version_list)}')
        version_uri = max(like_version_list)
        return version_uri

    def get_system_download_url(self):
        version_uri = self.get_driver_uri()
        self.open_url(DRIVER_DOWNLOAD_URL + version_uri)
        ele = self.browser.find_elements(By.XPATH, '/html/body/table/tbody/tr/td/a')
        url_list = [e.get_attribute('href') for e in ele]
        logger.info(f'[web] url_list: {url_list}')

        if platform.system() == "Windows":
            download_url = [d_url for d_url in url_list if "win" in d_url][0]
        elif platform.system() == "Linux":
            download_url = [d_url for d_url in url_list if "linux" in d_url][0]
        else:
            download_url = None
        logger.info(f'[web] download_url: {download_url}')
        return download_url

    def download_driver_to_local(self):
        download_url = self.get_system_download_url()
        rsp = requests.get(download_url)
        # 删除本地driver文件
        driver_path = self.get_driver_path_by_sys()
        # 关闭占用
        self.close()
        driver_dir = os.path.dirname(driver_path)
        logger.info(f'[web] dir content: {os.listdir(driver_dir)}')
        p_pid = get_pid("chromedriver.exe")
        if p_pid:
            os.kill(p_pid, signal.SIGINT)
        os.unlink(driver_path)
        # 写入下载的文件
        file_name = download_url.rsplit("/", 1)[-1]
        file_dir = os.path.join(driver_dir, file_name)
        with open(file_dir, mode='wb') as f:
            f.write(rsp.content)
        logger.info(f'[web] dir content: {os.listdir(driver_dir)}')
        # 解压文件到当前目录
        unzip_file(file_dir, driver_dir)


if __name__ == '__main__':
    action = BaseAction(mode=0)
    action.download_driver_to_local()

 

posted @ 2022-08-19 20:49  代码诠释的世界  阅读(262)  评论(0编辑  收藏  举报