在python selenium中打开指定路径的谷歌浏览器和驱动,有几种方法可以实现:
方法1:使用 executable_path 参数(传统方式)
from selenium import webdriver
from selenium.webdriver.chrome.service import service
# 指定浏览器和驱动的路径
chrome_path = r"c:\program files\google\chrome\application\chrome.exe" # 浏览器路径
driver_path = r"c:\path\to\chromedriver.exe" # 驱动路径
# 配置浏览器选项
options = webdriver.chromeoptions()
options.binary_location = chrome_path # 指定浏览器可执行文件路径
# 创建驱动服务
service = service(executable_path=driver_path)
# 启动浏览器
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"
# 配置选项
options = webdriver.chromeoptions()
options.binary_location = chrome_binary_path
# 创建服务并启动
service = chromeservice(executable_path=chromedriver_path)
driver = webdriver.chrome(service=service, options=options)
driver.get("https://www.example.com")
方法3:自动检测多个浏览器版本
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import service
def get_chrome_drivers():
"""获取系统中可能的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):
"""打开指定路径的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:
# 使用第一个找到的浏览器,或者你可以让用户选择
selected_browser = browsers[0]
driver_path = r"c:\path\to\chromedriver.exe" # 对应的驱动路径
driver = open_specific_chrome(selected_browser, driver_path)
driver.get("https://www.baidu.com")
方法4:处理用户数据目录
from selenium import webdriver
from selenium.webdriver.chrome.service import service
def open_chrome_with_profile(browser_path, driver_path, profile_path=none):
"""打开指定浏览器并可选地使用特定用户配置文件"""
options = webdriver.chromeoptions()
options.binary_location = browser_path
# 如果需要使用特定的用户配置文件
if profile_path:
options.add_argument(f"--user-data-dir={profile_path}")
# 其他常用选项
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):
"""启动指定路径的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驱动未找到: {driver_path}")
# 创建选项
chrome_options = webdriver.chromeoptions()
chrome_options.binary_location = browser_path
# 添加自定义选项
if options:
for option in options:
chrome_options.add_argument(option)
# 创建服务
service = service(executable_path=driver_path)
# 启动浏览器
self.driver = webdriver.chrome(service=service, options=chrome_options)
return self.driver
def close(self):
"""关闭浏览器"""
if self.driver:
self.driver.quit()
# 使用示例
manager = chromemanager()
# 配置选项
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()
注意事项:
- 版本匹配:确保chrome浏览器版本与chromedriver版本匹配
- 路径格式:使用原始字符串(r"path")或双反斜杠避免转义问题
- 权限:确保有足够的权限访问浏览器和驱动文件
- 杀毒软件:某些杀毒软件可能会阻止selenium操作
选择适合你需求的方法,方法2是目前最推荐的方式,因为它使用了selenium 4的最新语法。
到此这篇关于python selenium打开指定路径浏览器的几种实现方法的文章就介绍到这了,更多相关python selenium打开指定路径浏览器内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论