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
:
[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
:
[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
:
[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=])
:
[color=](
[color=]"Driver file already exists at:"
,
driver_path
)
[color=]
return
[color=]
[color=](
[color=]"Driver file does not exist, start downloading..."
)
[color=]
downloaded_path
=
manager_class
[color=]()
.install()
[color=]
[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
:
[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=]
[color=](
[color=]f"Default Browser:
[color=]{
default_browser
[color=]}
[color=]"
)
[color=]
default_browser_version = get_browser_version
[color=](
default_browser)
[color=]
[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=]
[color=](
[color=]"Current working directory:"
, os.getcwd
[color=]()
)