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

在GitHub Actions中使用RSelenium下载文件失败,如何解决?

问题:GitHub Actions中RSelenium搭配Docker容器无法下载文件

我在GitHub上维护一个测试仓库,尝试用GitHub Actions结合RSelenium与Selenium Firefox Docker容器访问指定URL并下载.zip文件。已配置卷映射,将仓库的data/downloads目录与容器内Firefox的下载目录/home/seluser/Downloads关联,本地测试时文件能正常下载并同步到本地目录,但在GitHub Actions中执行时,脚本可正常连接网站,却没有文件被下载。

GitHub Actions配置文件

on:
  push:
    branches: main

jobs:
  testFlow:
    runs-on: ubuntu-latest
    
    services:
      selenium:
        image: selenium/standalone-firefox:111.0-20230328
        ports:
          - '4444:4444'
        volumes:
          - /github/workspace/data/downloads:/home/seluser/Downloads

    steps:
      - name: Set up Docker
        uses: actions/checkout@v2
          
      # Load repo and install R and renv
      - uses: actions/checkout@v4
      - uses: r-lib/actions/setup-r@v2
        with:
          r-version: 'renv'
      - uses: r-lib/actions/setup-renv@v2

      - name: Install libcurl
        run: sudo apt-get install libcurl4-openssl-dev

      # Run R script
      - name: Script
        run: Rscript scripts/test.R

测试用R脚本

fprof <- RSelenium::makeFirefoxProfile(
  list(
    browser.download.dir = "/home/seluser/Downloads"
  )
)

remDr <- RSelenium::remoteDriver(
  remoteServerAddr = "localhost",
  port = 4444L,
  browserName = "firefox",
  extraCapabilities = fprof
)

dir.create(here::here("data", "downloads"), recursive = TRUE)

remDr$open(silent = TRUE)

remDr$navigate("https://demo.borland.com/testsite/download_testpage.php")

Sys.sleep(5)

print(remDr$getTitle())
print(remDr$getCurrentUrl())

remDr$findElement("xpath", "//tr[@filename='Small.zip']//td")$clickElement()
remDr$findElement("xpath", "//button[@id='downloadButton']")$clickElement()

remDr$close()

print(list.dirs("data", recursive = TRUE))
print(list.files("data", recursive = TRUE))

问题排查与修复方案

1. 卷映射与权限问题

GitHub Actions的服务容器与runner是隔离的,直接映射/github/workspace可能存在权限或路径识别问题。需要确保runner侧的下载目录存在且容器用户有写入权限:

  • 删除重复的actions/checkout步骤(保留v4版本即可)
  • 添加步骤创建下载目录并设置权限
  • 用变量${{ github.workspace }}指定绝对路径,避免路径歧义

修改后的steps片段:

steps:
  - uses: actions/checkout@v4
      
  - name: Prepare download directory
    run: |
      mkdir -p ${{ github.workspace }}/data/downloads
      sudo chmod 777 ${{ github.workspace }}/data/downloads

  # 后续R环境配置步骤不变...

同时更新services中的volumes配置:

volumes:
  - ${{ github.workspace }}/data/downloads:/home/seluser/Downloads

2. Firefox下载配置不完整

仅设置下载目录不足以在无头模式下自动完成下载,需添加禁用弹窗、指定自动下载的文件类型等配置:

修改R脚本中的Firefox profile:

fprof <- RSelenium::makeFirefoxProfile(
  list(
    browser.download.dir = "/home/seluser/Downloads",
    browser.download.folderList = 2, # 启用自定义下载目录
    browser.download.manager.showWhenStarting = FALSE, # 隐藏下载管理器弹窗
    browser.helperApps.neverAsk.saveToDisk = "application/zip,application/x-zip-compressed" # 指定zip文件自动下载
  )
)

3. 等待时间不足

固定的Sys.sleep(5)可能不足以完成文件下载,需替换为循环等待逻辑,确保文件下载完成后再关闭浏览器:

在点击下载按钮后添加:

# 等待文件下载完成,最长等待30秒
max_wait <- 30
wait_elapsed <- 0
target_file <- here::here("data", "downloads", "Small.zip")

while (wait_elapsed < max_wait) {
  if (file.exists(target_file)) {
    cat("文件下载完成\n")
    break
  }
  Sys.sleep(2)
  wait_elapsed <- wait_elapsed + 2
}

# 超时判断
if (wait_elapsed >= max_wait) {
  stop("文件下载超时")
}

4. Selenium服务访问地址优化

在GitHub Actions的服务网络中,直接使用服务名称selenium访问比localhost更可靠,修改R脚本中的remoteDriver配置:

remDr <- RSelenium::remoteDriver(
  remoteServerAddr = "selenium", # 替换为服务容器名称
  port = 4444L,
  browserName = "firefox",
  extraCapabilities = fprof
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 14:59:54