Python Selenium打開(kāi)指定路徑瀏覽器的幾種實(shí)現(xiàn)方法
在Python Selenium中打開(kāi)指定路徑的谷歌瀏覽器和驅(qū)動(dòng),有幾種方法可以實(shí)現(xiàn):
方法1:使用 executable_path 參數(shù)(傳統(tǒng)方式)
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# 指定瀏覽器和驅(qū)動(dòng)的路徑
chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe" # 瀏覽器路徑
driver_path = r"C:\path\to\chromedriver.exe" # 驅(qū)動(dòng)路徑
# 配置瀏覽器選項(xiàng)
options = webdriver.ChromeOptions()
options.binary_location = chrome_path # 指定瀏覽器可執(zhí)行文件路徑
# 創(chuàng)建驅(qū)動(dòng)服務(wù)
service = Service(executable_path=driver_path)
# 啟動(dòng)瀏覽器
driver = webdriver.Chrome(service=service, options=options)
# 使用瀏覽器
driver.get("https://www.baidu.com")
方法2:使用 Service 類(推薦,Selenium 4+)
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
# 指定路徑
chrome_binary_path = r"D:\AnotherChrome\Application\chrome.exe"
chromedriver_path = r"D:\drivers\chromedriver.exe"
# 配置選項(xiàng)
options = webdriver.ChromeOptions()
options.binary_location = chrome_binary_path
# 創(chuàng)建服務(wù)并啟動(dòng)
service = ChromeService(executable_path=chromedriver_path)
driver = webdriver.Chrome(service=service, options=options)
driver.get("https://www.example.com")
方法3:自動(dòng)檢測(cè)多個(gè)瀏覽器版本
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
def get_chrome_drivers():
"""獲取系統(tǒng)中可能的Chrome瀏覽器路徑"""
possible_paths = [
r"C:\Program Files\Google\Chrome\Application\chrome.exe",
r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
r"D:\Google\Chrome\Application\chrome.exe",
# 添加其他可能的路徑
]
existing_browsers = []
for path in possible_paths:
if os.path.exists(path):
existing_browsers.append(path)
return existing_browsers
def open_specific_chrome(browser_path, driver_path):
"""打開(kāi)指定路徑的Chrome瀏覽器"""
options = webdriver.ChromeOptions()
options.binary_location = browser_path
service = Service(executable_path=driver_path)
driver = webdriver.Chrome(service=service, options=options)
return driver
# 使用示例
if __name__ == "__main__":
# 顯示所有可用的Chrome瀏覽器
browsers = get_chrome_drivers()
print("找到的Chrome瀏覽器:")
for i, browser in enumerate(browsers):
print(f"{i+1}. {browser}")
# 選擇要使用的瀏覽器
if browsers:
# 使用第一個(gè)找到的瀏覽器,或者你可以讓用戶選擇
selected_browser = browsers[0]
driver_path = r"C:\path\to\chromedriver.exe" # 對(duì)應(yīng)的驅(qū)動(dòng)路徑
driver = open_specific_chrome(selected_browser, driver_path)
driver.get("https://www.baidu.com")
方法4:處理用戶數(shù)據(jù)目錄
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
def open_chrome_with_profile(browser_path, driver_path, profile_path=None):
"""打開(kāi)指定瀏覽器并可選地使用特定用戶配置文件"""
options = webdriver.ChromeOptions()
options.binary_location = browser_path
# 如果需要使用特定的用戶配置文件
if profile_path:
options.add_argument(f"--user-data-dir={profile_path}")
# 其他常用選項(xiàng)
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
service = Service(executable_path=driver_path)
driver = webdriver.Chrome(service=service, options=options)
# 隱藏webdriver屬性
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
return driver
# 使用示例
browser_path = r"C:\Custom\Chrome\Application\chrome.exe"
driver_path = r"C:\Custom\Drivers\chromedriver.exe"
profile_path = r"C:\Users\YourName\AppData\Local\Google\Chrome\User Data\Profile 1"
driver = open_chrome_with_profile(browser_path, driver_path, profile_path)
driver.get("https://www.example.com")
方法5:完整的配置類
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
class ChromeManager:
def __init__(self):
self.driver = None
def start_chrome(self, browser_path, driver_path, options=None):
"""啟動(dòng)指定路徑的Chrome瀏覽器"""
if not os.path.exists(browser_path):
raise FileNotFoundError(f"Chrome瀏覽器未找到: {browser_path}")
if not os.path.exists(driver_path):
raise FileNotFoundError(f"Chrome驅(qū)動(dòng)未找到: {driver_path}")
# 創(chuàng)建選項(xiàng)
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = browser_path
# 添加自定義選項(xiàng)
if options:
for option in options:
chrome_options.add_argument(option)
# 創(chuàng)建服務(wù)
service = Service(executable_path=driver_path)
# 啟動(dòng)瀏覽器
self.driver = webdriver.Chrome(service=service, options=chrome_options)
return self.driver
def close(self):
"""關(guān)閉瀏覽器"""
if self.driver:
self.driver.quit()
# 使用示例
manager = ChromeManager()
# 配置選項(xiàng)
custom_options = [
"--disable-extensions",
"--no-sandbox",
"--disable-dev-shm-usage",
"--start-maximized"
]
try:
driver = manager.start_chrome(
browser_path=r"C:\Program Files\Google\Chrome\Application\chrome.exe",
driver_path=r"C:\webdrivers\chromedriver.exe",
options=custom_options
)
driver.get("https://www.baidu.com")
finally:
manager.close()
注意事項(xiàng):
- 版本匹配:確保Chrome瀏覽器版本與chromedriver版本匹配
- 路徑格式:使用原始字符串(r"path")或雙反斜杠避免轉(zhuǎn)義問(wèn)題
- 權(quán)限:確保有足夠的權(quán)限訪問(wèn)瀏覽器和驅(qū)動(dòng)文件
- 殺毒軟件:某些殺毒軟件可能會(huì)阻止Selenium操作
選擇適合你需求的方法,方法2是目前最推薦的方式,因?yàn)樗褂昧薙elenium 4的最新語(yǔ)法。
到此這篇關(guān)于Python Selenium打開(kāi)指定路徑瀏覽器的幾種實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Python Selenium打開(kāi)指定路徑瀏覽器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python爬蟲(chóng)控制aiohttp并發(fā)數(shù)量方式
這篇文章主要介紹了python爬蟲(chóng)控制aiohttp并發(fā)數(shù)量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Python MySQLdb 執(zhí)行sql語(yǔ)句時(shí)的參數(shù)傳遞方式
這篇文章主要介紹了Python MySQLdb 執(zhí)行sql語(yǔ)句時(shí)的參數(shù)傳遞方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Django中STATIC_ROOT和STATIC_URL及STATICFILES_DIRS淺析
這篇文章主要給大家介紹了關(guān)于Django中STATIC_ROOT和STATIC_URL及STATICFILES_DIRS的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-05-05
Python數(shù)據(jù)可視化實(shí)現(xiàn)多種圖例代碼詳解
這篇文章主要介紹了Python數(shù)據(jù)可視化實(shí)現(xiàn)多種圖例代碼詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Django中文件上傳和文件訪問(wèn)微項(xiàng)目的方法
這篇文章主要介紹了Django中文件上傳和文件訪問(wèn)微項(xiàng)目的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
學(xué)習(xí)python 的while循環(huán)嵌套
這篇文章主要為大家介紹了python 的while循環(huán)嵌套,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-12-12
Python re.split方法分割字符串的實(shí)現(xiàn)示例
本文主要介紹了Python re.split方法分割字符串的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

