吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 740|回复: 18
收起左侧

[Python 原创] 使用selenium 自动根据电脑浏览器下载自动化驱动

  [复制链接]
xiaqiusheng 发表于 2024-11-1 14:39
本帖最后由 苏紫方璇 于 2024-11-3 23:38 编辑

[Python] 纯文本查看 复制代码
import os
import time
import winreg
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.microsoft import IEDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager

BROWSER_PATH = {
    "chrome": r"SOFTWARE\Google\Chrome\BLBeacon",
    "firefox": r"SOFTWARE\mozilla.org\Mozilla",
    "ie": r"SOFTWARE\Microsoft\Internet Explorer",
    "edge": r"SOFTWARE\Microsoft\Edge\BLBeacon",
}

DRIVER_DOWNLOAD = {
    "chrome": (ChromeDriverManager, "chromedriver.exe"),
    "firefox": (GeckoDriverManager, "geckodriver.exe"),
    "ie": (IEDriverManager, "IEDriverServer.exe"),
    "edge": (EdgeChromiumDriverManager, "msedgedriver.exe"),
}

DRIVER_CLASS = {
    "chrome": webdriver.Chrome,
    "firefox": webdriver.Firefox,
    "ie": webdriver.Ie,
    "edge": webdriver.Edge,
}


def get_default_browser():
    key = r"SOFTWARE\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice"
    try:
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, key) as reg_key:
            value, _ = winreg.QueryValueEx(reg_key, "Progid")
            for browser in BROWSER_PATH.keys():
                if browser in value.lower():
                    return browser
    except Exception as e:
        print(f"Error retrieving default browser: {e}")
    return None


def get_browser_version(default_browser):
    key = BROWSER_PATH.get(default_browser)
    if key is None:
        return None
    try:
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, key) as reg_key:
            if default_browser == "firefox":
                value, _ = winreg.QueryValueEx(reg_key, "CurrentVersion")
            else:
                value, _ = winreg.QueryValueEx(reg_key, "version")
            return value
    except Exception as e:
        print(f"Error retrieving browser version for {default_browser}: {e}")
    return None


def check_and_download_driver(browser):
    manager_class, filename = DRIVER_DOWNLOAD.get(browser, (None, None))
    if manager_class is None:
        print(f"Driver for {browser} not supported.")
        return

    #下载到根目录的drivers文件夹下

    driver_directory  = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'drivers', browser)

    if not os.path.exists(driver_directory):
        os.makedirs(driver_directory)

    driver_path = os.path.join(driver_directory, filename)
    if os.path.exists(driver_path):
        print("Driver file already exists at:", driver_path)
        return

    print("Driver file does not exist, start downloading...")
    downloaded_path = manager_class().install()
    print("Driver downloaded successfully at:", downloaded_path)


def open_browser(default_browser):
    driver_class = DRIVER_CLASS.get(default_browser)
    if driver_class is None:
        print(f"No driver class for {default_browser}")
        return

    # driver = driver_class()
    # driver.get("https://www.baidu.com")
    # while True:
    #     time.sleep(1)


if __name__ == "__main__":
    default_browser = get_default_browser()
    print(f"Default Browser: {default_browser}")
    default_browser_version = get_browser_version(default_browser)
    print(f"Default Browser Version: {default_browser_version}")
    check_and_download_driver(default_browser)
    open_browser(default_browser)
    print("Current working directory:", os.getcwd())

Snipaste_2024-11-01_14-39-53.png

免费评分

参与人数 2吾爱币 +8 热心值 +2 收起 理由
nullable + 1 + 1 谢谢@Thanks!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

TotheAnts 发表于 2024-11-13 09:59
可以,终于不用手动下载驱动了
HapplyLin 发表于 2024-11-12 09:59
苏紫方璇 发表于 2024-11-1 16:22
插入代码可以参考这个帖子
【公告】发帖代码插入以及添加链接教程(有福利)
https://www.52pojie.cn/thread-713042-1-1.html
(出处: 吾爱破解论坛)
linlin01 发表于 2024-11-10 09:20
好东西啊,值得收藏下
nullable 发表于 2024-11-9 18:09
前阵子做一些测试的时候想用这个工具,结果下下来了现在才看到大佬的工具,已收藏
kinghanmin13 发表于 2024-11-7 11:03
试用一下看看
yuanhangwer 发表于 2024-11-11 20:41

好东西啊,值得收藏下
ziliansha 发表于 2024-11-1 17:50
加一个现有默认浏览器版本和已下载的浏览器版本对比,版本不匹配则下载最新的driver
hankv 发表于 2024-11-2 21:37
受教了。。。收藏下来看下
serpentking 发表于 2024-11-3 20:57
这个代码值得保留,先试用下。
a761199721 发表于 2024-11-4 09:14
当文件夹有驱动存在时,如果版本不匹配不能自动更新
 楼主| xiaqiusheng 发表于 2024-11-4 14:46
ziliansha 发表于 2024-11-1 17:50
加一个现有默认浏览器版本和已下载的浏览器版本对比,版本不匹配则下载最新的driver

已经是有这个功能了
 楼主| xiaqiusheng 发表于 2024-11-4 14:47
a761199721 发表于 2024-11-4 09:14
当文件夹有驱动存在时,如果版本不匹配不能自动更新

有旧的驱动,浏览器识别不了,ui自动化会失败,不能用不更新么
HR741158 发表于 2024-11-5 21:45
虽不知道有什么用,但还是要收藏
wangtietou 发表于 2024-11-6 11:43
chrome更新太快了,下driver也挺烦。
个人感觉drission比selenium更友好,起码不用下载driver
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 13:06

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表