优化浏览器查找能力
This commit is contained in:
@@ -154,8 +154,29 @@ def html_to_image(html_file, output_image):
|
||||
:param output_image: 输出的图片文件路径
|
||||
"""
|
||||
with sync_playwright() as p:
|
||||
# TODO 这里使用的比较呆的固定路径,后期优化
|
||||
browser = p.chromium.launch(executable_path=r"C:\Users\Liu_WIN10\AppData\Local\Google\Chrome\Application\chrome.exe")
|
||||
# 定义可能的Chrome浏览器路径列表
|
||||
possible_chrome_paths = [
|
||||
r"C:\Users\Liu_WIN10\AppData\Local\Google\Chrome\Application\chrome.exe",
|
||||
r"C:\Users\Liu-OPEN\AppData\Local\Google\Chrome\Application\chrome.exe",
|
||||
r"C:\Program Files\Google\Chrome\Application\chrome.exe",
|
||||
r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
|
||||
]
|
||||
|
||||
# 查找可用的浏览器路径
|
||||
browser_path = None
|
||||
for path in possible_chrome_paths:
|
||||
if os.path.exists(path):
|
||||
browser_path = path
|
||||
print(f"找到浏览器路径: {browser_path}")
|
||||
break
|
||||
|
||||
# 如果没有找到可用的路径,则使用默认启动方式
|
||||
if browser_path:
|
||||
browser = p.chromium.launch(executable_path=browser_path)
|
||||
|
||||
else:
|
||||
print("未找到指定的Chrome浏览器路径,使用默认浏览器")
|
||||
browser = p.chromium.launch()
|
||||
page = browser.new_page()
|
||||
|
||||
# 加载本地的 HTML 文件
|
||||
|
||||
Reference in New Issue
Block a user