You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins环境下Java Selenium批量上传PDF异常问题求助

Java/Selenium PDF上传异常排查求助

问题现象

  • 本地IDE运行时,下载文件夹可正常获取2个待上传的PDF文件,连续上传无异常
  • Jenkins环境中,下载文件夹仅存在第一个PDF文件;单独上传第二个PDF可成功,连续上传则失败

Java代码配置

private static void launchChromeDocker() {
    System.setProperty("webdriver.chrome.driver", "/usr/lib/chromium/chromedriver");
    final ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments(
            "lang=fr-FR",
            "--no-sandbox",
            "--disable-dev-shm-usage",
            "--disable-gpu",
            "--disable-web-security",
            "user-data-dir=/profile_pushbullet",
            "load-extension=/profile_pushbullet/Default/Extensions/chlffgpmiacpedhhbkiomidkjlcfhogd/364_0",
            "--remote-allow-origins=*",
            "--ignore-certificate-errors"
    );

    final Map<String, Object> chromePrefs = new HashMap<>();
    chromePrefs.put("profile.exit_type", "Normal");        

        final String parentDirectoryPath = System.getProperty("user.dir");
        final String downloadFilepath = parentDirectoryPath + "/downloads/";
        logger.info("Chrome Download path set to: " + downloadFilepath);
        final File downloadFolder = new File(downloadFilepath);
        if (!downloadFolder.exists()) {
            downloadFolder.mkdir();
        }

    chromePrefs.put("download.default_directory", downloadFilepath);
    chromePrefs.put("download.prompt_for_download", false);       
    
    chromeOptions.setExperimentalOption("prefs", chromePrefs);
    chromeOptions.setAcceptInsecureCerts(true);
    chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);

    final DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
    desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
    
    driver = new ChromeDriver(chromeOptions);
}

Jenkins制品配置

Monitoring/**/**/*.html, Monitoring/**/**/screenshots/*.png, loan/*.txt, **/**.pdf

排查方向

  1. 补充文件下载完成等待逻辑
    连续上传时,Jenkins环境磁盘IO可能比本地慢,第二个PDF可能未完成下载就触发上传。需添加等待判断:

    • 检查文件存在且大小不再变化(避免捕获未下载完成的临时文件)
    • 示例判断代码:
      public static void waitForFileDownload(String filePath, int timeoutSeconds) {
          File file = new File(filePath);
          long lastSize = 0;
          long startTime = System.currentTimeMillis();
          while (System.currentTimeMillis() - startTime < timeoutSeconds * 1000) {
              if (file.exists()) {
                  long currentSize = file.length();
                  if (currentSize == lastSize && currentSize > 0) {
                      break;
                  }
                  lastSize = currentSize;
              }
              try {
                  Thread.sleep(500);
              } catch (InterruptedException e) {
                  Thread.currentThread().interrupt();
              }
          }
      }
      
  2. 检查Jenkins工作空间权限

    • 确认Jenkins运行用户对downloads目录有读写权限
    • 可在Jenkins构建步骤中添加ls -l downloads/命令,打印目录内容和权限信息
  3. 验证Chrome下载路径一致性

    • Jenkins环境中System.getProperty("user.dir")可能与本地不同,对比控制台输出的下载路径和实际工作空间路径是否一致
  4. 排查Chrome配置冲突

    • 临时移除user-data-dir和load-extension参数测试,确认是否是用户配置或扩展影响下载行为
  5. 调整Jenkins制品归档时机

    • 确保测试代码完成所有下载操作后,再触发Jenkins的制品归档流程,可添加延迟或等待步骤

内容的提问来源于stack exchange,提问作者Clay_2_treize

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 17:56:17