使用selenium 自动根据电脑浏览器下载自动化驱动

查看 32|回复 3
作者:xiaqiusheng   
[color=]import
os
[color=]import
time
[color=]import
winreg
[color=]from
selenium
[color=]import
webdriver
[color=]from
webdriver_manager.chrome
[color=]import
ChromeDriverManager
[color=]from
webdriver_manager.firefox
[color=]import
GeckoDriverManager
[color=]from
webdriver_manager.microsoft
[color=]import
IEDriverManager
[color=]from
webdriver_manager.microsoft
[color=]import
EdgeChromiumDriverManager
BROWSER_PATH = {
[color=]   
[color=]"chrome"
:
[color=]r"SOFTWARE\Google\Chrome\BLBeacon"
,
   
[color=]"firefox"
:
[color=]r"SOFTWARE\mozilla.org\Mozilla"
,
   
[color=]"ie"
:
[color=]r"SOFTWARE\Microsoft\Internet Explorer"
,
   
[color=]"edge"
:
[color=]r"SOFTWARE\Microsoft\Edge\BLBeacon"
,
}
DRIVER_DOWNLOAD = {
[color=]   
[color=]"chrome"
:
[color=](
ChromeDriverManager,
[color=]"chromedriver.exe"
[color=])
,
   
[color=]"firefox"
:
[color=](
GeckoDriverManager,
[color=]"geckodriver.exe"
[color=])
,
   
[color=]"ie"
:
[color=](
IEDriverManager,
[color=]"IEDriverServer.exe"
[color=])
,
   
[color=]"edge"
:
[color=](
EdgeChromiumDriverManager,
[color=]"msedgedriver.exe"
[color=])
,
}
DRIVER_CLASS = {
[color=]   
[color=]"chrome"
: webdriver.Chrome,
   
[color=]"firefox"
: webdriver.Firefox,
   
[color=]"ie"
: webdriver.Ie,
   
[color=]"edge"
: webdriver.Edge,
}
[color=]def
[color=]get_default_browser
[color=]()
:
   
key
= r"SOFTWARE\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice"
[color=]   
[color=]try
:
        
[color=]with
winreg.OpenKey
[color=](
winreg.HKEY_CURRENT_USER,
key
[color=])
[color=]as
reg_key
:
            
value
, _ = winreg.QueryValueEx
[color=](
reg_key
,
[color=]"Progid"
)
[color=]            
[color=]for
browser
[color=]in
BROWSER_PATH.keys
[color=]()
:
               
[color=]if
browser
[color=]in
value
.lower
[color=]()
:
                    
[color=]return
browser
   
[color=]except
Exception
[color=]as
e
:
        
print
[color=](
[color=]f"Error retrieving default browser:
[color=]{
e
[color=]}
[color=]"
)
[color=]   
return None
[color=]def
[color=]get_browser_version
[color=](
default_browser
[color=])
:
   
key
= BROWSER_PATH.get
[color=](
default_browser
)
[color=]   
[color=]if
key
[color=]is None
:
        return None
[color=]    try
:
        
[color=]with
winreg.OpenKey
[color=](
winreg.HKEY_CURRENT_USER,
key
[color=])
[color=]as
reg_key
:
            
[color=]if
default_browser
==
[color=]"firefox"
:
               
value
, _ = winreg.QueryValueEx
[color=](
reg_key
,
[color=]"CurrentVersion"
)
[color=]            
[color=]else
:
               
value
, _ = winreg.QueryValueEx
[color=](
reg_key
,
[color=]"version"
)
[color=]            
[color=]return
value
   
[color=]except
Exception
[color=]as
e
:
        
print
[color=](
[color=]f"Error retrieving browser version for
[color=]{
default_browser
[color=]}
[color=]:
[color=]{
e
[color=]}
[color=]"
)
[color=]   
return None
[color=]def
[color=]check_and_download_driver
[color=](
browser
[color=])
:
   
manager_class
,
filename
= DRIVER_DOWNLOAD.get
[color=](
browser
,
[color=](
[color=]None
,
[color=]None
[color=])
)
[color=]   
[color=]if
manager_class
[color=]is None
:
        
print
[color=](
[color=]f"Driver for
[color=]{
browser
[color=]}
[color=] not supported."
)
[color=]        
return
[color=]   
#下载到根目录的drivers文件夹下

   
driver_directory  
= os.path.join
[color=](
os.path.dirname
[color=](
os.path.abspath
[color=](
__file__
[color=])
[color=])
,
[color=]'drivers'
,
browser
)
[color=]   
[color=]if not
os.path.exists
[color=](
driver_directory
[color=])
:
        os.makedirs
[color=](
driver_directory
)
[color=]   
driver_path
= os.path.join
[color=](
driver_directory
,
filename
)
[color=]   
[color=]if
os.path.exists
[color=](
driver_path
[color=])
:
        
print
[color=](
[color=]"Driver file already exists at:"
,
driver_path
)
[color=]        
return
[color=]   
print
[color=](
[color=]"Driver file does not exist, start downloading..."
)
[color=]   
downloaded_path
=
manager_class
[color=]()
.install()
[color=]   
print
[color=](
[color=]"Driver downloaded successfully at:"
,
downloaded_path
)
[color=]def
[color=]open_browser
[color=](
default_browser
[color=])
:
   
driver_class
= DRIVER_CLASS.get
[color=](
default_browser
)
[color=]   
[color=]if
driver_class
[color=]is None
:
        
print
[color=](
[color=]f"No driver class for
[color=]{
default_browser
[color=]}
[color=]"
)
[color=]        
return
[color=]   
# driver = driver_class()
    # driver.get("https://www.baidu.com")
    # while True:
    #     time.sleep(1)



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

浏览器, 电脑

ziliansha   

插入代码可以参考这个帖子
【公告】发帖代码插入以及添加链接教程(有福利)
https://www.52pojie.cn/thread-713042-1-1.html
(出处: 吾爱破解论坛)
爱美生活   

加一个现有默认浏览器版本和已下载的浏览器版本对比,版本不匹配则下载最新的driver
爱美生活   

就干啥结核杆菌
您需要登录后才可以回帖 登录 | 立即注册

返回顶部