有没有办法在Centos中使用Ruby使用headless chromedriver下载文件?

有没有办法在Centos中使用Ruby使用headless chromedriver下载文件?,ruby,selenium-webdriver,google-chrome-headless,Ruby,Selenium Webdriver,Google Chrome Headless,我尝试使用无头chrome下载一个文件,但该文件似乎没有被下载到任何地方。我可以看出,它实际上是一种安全特性,可以限制headless中的文件下载,但是,Ruby中是否有类似的解决方法?尝试了以下代码,但没有成功 download_path = "#{Pathname.pwd}/test-data/downloaded" options = Selenium::WebDriver::Chrome::Options.new options.add_argument("--disable-dev-s

我尝试使用无头chrome下载一个文件,但该文件似乎没有被下载到任何地方。我可以看出,它实际上是一种安全特性,可以限制headless中的文件下载,但是,Ruby中是否有类似的解决方法?尝试了以下代码,但没有成功

download_path = "#{Pathname.pwd}/test-data/downloaded"
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument("--disable-dev-shm-usage");
options.add_argument('--headless') #Declaring the browser to run in headless mode
options.add_preference(
  :download, directory_upgrade: true,
  prompt_for_download: false,
  default_directory: download_path
)
options.add_preference(:browser, set_download_behavior: { behavior: 'allow' })
@driver = Selenium::WebDriver.for :chrome, options: options #Browser object initialization
set_screen_resolution(1400, 900)
$driver = @driver

bridge = @driver.send(:bridge)
path = '/session/:session_id/chromium/send_command'
path[':session_id'] = bridge.session_id
bridge.http.call(:post, path, cmd: 'Page.setDownloadBehavior',
                        params: {
                          behavior: 'allow',
                          downloadPath: download_path
                        })

我希望使用headless chrome下载该文件,但这种情况不会发生。

当您单击下载链接时,如果在文件开始下载之前它在单独的选项卡中打开,那么您也需要将上述脚本应用于新打开的选项卡,因为您只为当前选项卡而不是新打开的选项卡设置了会话id。因此,在尝试下载文件之前,请尝试将此脚本应用于新打开的选项卡。我相信它会起作用的

def download_file(label, download_path)
  ele = Locator.new(:xpath, "//ul[@class='cnt']//div[text()='#{label}']/..//a")
  download_url = get_attribute(ele.get_how, ele.get_what, "href")
  @driver.execute_script("window.open()")
  @driver.switch_to.window(@driver.window_handles.last)
  bridge = @driver.send(:bridge)
  path = '/session/:session_id/chromium/send_command'
  path[':session_id'] = bridge.session_id
  bridge.http.call(:post, path, {
    "cmd" => 'Page.setDownloadBehavior',
    "params" => {
      "behavior" => 'allow',
      "downloadPath" => download_path
    }
  })
  @driver.get(download_url)
  @driver.close
  @driver.switch_to.window(@driver.window_handles.last)
end

当您单击下载链接时,如果它在文件开始下载之前在单独的选项卡中打开,那么您也需要将上述脚本应用于新打开的选项卡,因为您只为当前选项卡而不是新打开的选项卡设置了会话id。因此,在尝试下载文件之前,请尝试将此脚本应用于新打开的选项卡。我相信它会起作用的

def download_file(label, download_path)
  ele = Locator.new(:xpath, "//ul[@class='cnt']//div[text()='#{label}']/..//a")
  download_url = get_attribute(ele.get_how, ele.get_what, "href")
  @driver.execute_script("window.open()")
  @driver.switch_to.window(@driver.window_handles.last)
  bridge = @driver.send(:bridge)
  path = '/session/:session_id/chromium/send_command'
  path[':session_id'] = bridge.session_id
  bridge.http.call(:post, path, {
    "cmd" => 'Page.setDownloadBehavior',
    "params" => {
      "behavior" => 'allow',
      "downloadPath" => download_path
    }
  })
  @driver.get(download_url)
  @driver.close
  @driver.switch_to.window(@driver.window_handles.last)
end

Chromedriver的版本-74.0.3729是否在正常模式下发生?不,仅在无头模式下发生mode@Ardesco我需要一个ruby版本的ChromeDriver的版本-74.0.3729它是否在正常模式下发生?不,它只在无头模式下发生mode@Ardesco我需要一个ruby版本,非常感谢。成功了。将相同的脚本应用于刚刚解决问题的新打开的选项卡。您介意分享您如何将其应用于新打开的选项卡吗?我没有单击文件下载链接,而是获取该链接的href,打开一个新选项卡,应用所述脚本并导航到将下载文件的href。非常感谢。成功了。将相同的脚本应用于刚刚解决问题的新打开的选项卡。您介意分享您如何将其应用于新打开的选项卡吗?我没有单击文件下载链接,而是获取该链接的href,打开一个新选项卡,应用所述脚本并导航到将下载文件的href。