diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 2b262f4..8f158e3 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -13,7 +13,7 @@ - + diff --git a/.idea/git_toolbox_blame.xml b/.idea/git_toolbox_blame.xml index 7dc1249..f165acd 100644 --- a/.idea/git_toolbox_blame.xml +++ b/.idea/git_toolbox_blame.xml @@ -1,6 +1,10 @@ - \ No newline at end of file diff --git a/src/main/java/com/fantaibao/config/PlaywrightManager.java b/src/main/java/com/fantaibao/config/PlaywrightManager.java index bd62c69..4885fbf 100644 --- a/src/main/java/com/fantaibao/config/PlaywrightManager.java +++ b/src/main/java/com/fantaibao/config/PlaywrightManager.java @@ -1,8 +1,12 @@ package com.fantaibao.config; +import cn.hutool.core.util.StrUtil; import com.microsoft.playwright.Playwright; import lombok.extern.slf4j.Slf4j; +import java.util.HashMap; +import java.util.Map; + /** * Playwright单例管理类 * 用于全局管理和提供Playwright实例 @@ -18,7 +22,18 @@ public class PlaywrightManager { throw new RuntimeException("请使用getInstance()方法获取实例"); } try { - this.playwright = Playwright.create(); + String playwrightBrowsersPath = System.getenv("PLAYWRIGHT_BROWSERS_PATH"); + if (StrUtil.isBlank(playwrightBrowsersPath)) { + log.info("PLAYWRIGHT_BROWSERS_PATH环境变量未设置,将使用默认路径"); + playwright = Playwright.create(); + return; + } + Map env = new HashMap<>(); + env.put("PLAYWRIGHT_BROWSERS_PATH",playwrightBrowsersPath); + env.put("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD","1"); + Playwright.CreateOptions options = new Playwright.CreateOptions(); + options.setEnv(env); + this.playwright = Playwright.create(options); } catch (Exception e) { log.error("创建Playwright实例失败", e); throw new RuntimeException("创建Playwright实例失败", e);