当前位置: 首页 > article >正文

Selenium 浏览器驱动代理 - 无需下载本地浏览器驱动镜像!(Python 版本!)

文章目录

  • 概述
    • 传统频繁下载对应浏览器驱动镜像的烦恼
    • webdriver_manager 驱动代理
      • 安装方式
      • 具体使用方法
        • Use with Chrome
        • Use with Chromium
        • Use with Brave
        • Use with Edge
        • Use with Firefox
        • Use with IE
        • Use with Opera
      • 最佳实践方式

概述

传统频繁下载对应浏览器驱动镜像的烦恼

当我们使用selenium 浏览器驱动的时候,我们常用做法就是,针对不同的浏览器下载不同的镜像版本,然后再安装到 python 安装目录下,然后再进行浏览器访问;这样做的缺点在于:

当浏览器更新版本时候,你需要同步更新对应的浏览器驱动镜像,频繁维护
浏览器镜像源地址不稳定,有时候找不到对应的浏览器驱动镜像源头,下载缓慢

那么有没有一种方法,我不用频繁的安装对应的驱动,找镜像源,让第三方代理,我只需要写少量的代码即可,让它自动的去下载我对应的浏览器版本的驱动?

有的! 就是 webdriver_manager 这个第三方库!

在这里插入图片描述

webdriver_manager 驱动代理

当涉及到自动更新浏览器驱动时,Python提供了一些库来帮助实现这个功能。其中一个流行的库是webdriver_manager,它可以自动下载和更新浏览器驱动。

地址:驱动代理下载官网地址

For now support:

  • ChromeDriver
  • GeckoDriver
  • IEDriver
  • OperaDriver
  • EdgeChromiumDriver

Compatible with Selenium 4.x and below.

Before: You need to download the chromedriver binary, unzip it somewhere on your PC and set the path to this driver like this:

from selenium import webdriver
driver = webdriver.Chrome('/home/user/drivers/chromedriver')

It’s boring!!! Moreover, every time a new version of the driver is released, you need to repeat all these steps again and again.

With webdriver manager, you just need to do two simple steps:

安装方式

pip install webdriver-manager

具体使用方法

Use with Chrome
# selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
Use with Chromium
# selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType

driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())

# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType

driver = webdriver.Chrome(service=ChromiumService(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()))
Use with Brave
# selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType

driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install())

# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as BraveService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType

driver = webdriver.Chrome(service=BraveService(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install()))
Use with Edge
# selenium 3
from selenium import webdriver
from webdriver_manager.microsoft import EdgeChromiumDriverManager

driver = webdriver.Edge(EdgeChromiumDriverManager().install())

# selenium 4
from selenium import webdriver
from selenium.webdriver.edge.service import Service as EdgeService
from webdriver_manager.microsoft import EdgeChromiumDriverManager

driver = webdriver.Edge(service=EdgeService(EdgeChromiumDriverManager().install()))
Use with Firefox
# selenium 3
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

# selenium 4
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
Use with IE
# selenium 3
from selenium import webdriver
from webdriver_manager.microsoft import IEDriverManager

driver = webdriver.Ie(IEDriverManager().install())

# selenium 4
from selenium import webdriver
from selenium.webdriver.ie.service import Service as IEService
from webdriver_manager.microsoft import IEDriverManager

driver = webdriver.Ie(service=IEService(IEDriverManager().install()))
Use with Opera
# selenium 3
from selenium import webdriver
from selenium.webdriver.chrome import service
from webdriver_manager.opera import OperaDriverManager

webdriver_service = service.Service(OperaDriverManager().install())
webdriver_service.start()

driver = webdriver.Remote(webdriver_service.service_url, webdriver.DesiredCapabilities.OPERA)

# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome import service
from webdriver_manager.opera import OperaDriverManager

webdriver_service = service.Service(OperaDriverManager().install())
webdriver_service.start()

options = webdriver.ChromeOptions()
options.add_experimental_option('w3c', True)

driver = webdriver.Remote(webdriver_service.service_url, options=options)

If the Opera browser is installed in a location other than C:/Program Files or C:/Program Files (x86) on Windows and /usr/bin/opera for all unix variants and mac, then use the below code,

options = webdriver.ChromeOptions()
options.binary_location = "path/to/opera.exe"
driver = webdriver.Remote(webdriver_service.service_url, options=options)

最佳实践方式

假设我们创建了一个 WebDriverUtil工具类,使用浏览器为谷歌浏览器,那么你就可以在你的WebDriverUtil中如下写法:

import logging
import os

import emoji
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from api import SYSTEM_URL



class WebDriverUtil:

    _driver = None

    """
    This is a method about get WebDriver.

    """

    @classmethod
    def get_driver(cls):
        if cls._driver is None:
            logging.info(emoji.emojize(":gear:  启动自动化测试", language="alias"))
            logging.info(emoji.emojize(":hourglass:  正在初始化驱动环境...", language="alias"))
            # 在这里使用 浏览器驱动对象代理
            cls._driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
            logging.info(emoji.emojize(":sparkles: :sparkles: :sparkles: :sparkles: :sparkles:  浏览器驱动加载完成!", language="alias"))
            logging.info(emoji.emojize(":hourglass:  开始访问目标链接...", language="alias"))
            cls._driver.maximize_window()
            # Get access path
            cls._driver.get(SYSTEM_URL)
        return cls._driver

    """
    This is a method about quit WebDriver.
    """

    @classmethod
    def quit_driver(cls):
        if cls._driver is not None:
            cls.get_driver().quit()
            cls._driver = None

http://www.kler.cn/a/459334.html

相关文章:

  • Linux-掉电保护方案
  • Postman[8] 断言
  • Docker 安装与配置 Nginx
  • 使用 Actix-Web、SQLx 和 Redis 构建高性能 Rust Web 服务
  • 嵌入式入门Day35
  • Mac电脑python多版本环境安装与切换
  • 3.从制定标准到持续监控:7个关键阶段提升App用户体验
  • 【Python高级374】正则表达式
  • 深入解析MySQL索引结构:从数组到B+树的演变与优化
  • 【2024年-8月-6日-开源社区openEuler实践记录】ChatIG:开启智能交互新体验的开源项目
  • 21. 【.NET 8 实战--孢子记账--从单体到微服务】--简易权限--补充--自动添加角色可访问接口
  • Unity WebGL 部署IIS
  • SQL 实战:基于经纬度的距离计算与位置查询
  • 爬虫后的数据处理与使用(处理篇)
  • 数据结构(Java)——链表
  • gala-gopher
  • 在Windows10下安装Docker WSL 2 桌面版
  • 基于python大数据的图书销售系统
  • Flutter:打包apk,详细图文介绍
  • QT-----------GUI程序设计基础
  • 基于Arduino的音乐喷泉设计(论文+源码)
  • echarts:5、树状图
  • C++类与对象(三)-- 再谈构造函数(细嗦初始化列表)、static成员
  • 多进程并发执行,多线程并发服务器
  • Redis中的数据类型
  • 数据结构与算法学习笔记----约数