ruby 如何从本地计算机访问远程客户端下载的文件

92dk7w1h  于 2023-04-20  发布在  Ruby
关注(0)|答案(1)|浏览(102)

我有一个Selenium浏览器配置设置,它具有以下设置:

download_path = '/Users/my.user/Downloads'

# Setting capabilities for my browser
caps = Selenium::WebDriver::Remote::Capabilities.chrome
caps['goog:chromeOptions'] = {
  'prefs' => {
    'download.default_directory' => download_path,
    'download.prompt_for_download' => false,
    'profile.default_content_settings.popups' => 0
  },
  'args' => [
    # 'disable-dev-shm-usage',
    'no-sandbox',
    'disable-popup-blocking',
    'window-size=1400,900',
    'disable-extensions',
    'disable-gpu'
  ]
}
caps['goog:chromeOptions']['prefs']['plugins.always_open_pdf_externally'] = true

# Trying to run this in AWS DeviceFarm
devicefarm = Aws::DeviceFarm::Client.new(region: 'us-west-2', access_key_id: ACCESS_KEY_ID,
                                         secret_access_key: SECRET_ACCESS_KEY)
test_grid_url_response = devicefarm.create_test_grid_url(project_arn: ARN, expires_in_seconds: 3000)
remote_url = test_grid_url_response.url

# Setting up a HTTP client to run it in DeviceFarm
client = Selenium::WebDriver::Remote::Http::Default.new
client.read_timeout = 5000

# Setting up the browser
driver = Selenium::WebDriver.for :remote, http_client: client, url: remote_url, capabilities: caps
url = 'https://www.google.com/'
driver.manage.timeouts.implicit_wait = 30

我试图从本地计算机访问Windows远程计算机上下载的文件,但无法访问。
我为文件上传写了一个lambda,它是这样的:

driver.file_detector = lambda do |args|
          str = args.first.to_s
          str if File.exist?(str)
        end

但我无法从本地计算机访问任何远程下载的文件。
有没有人能提出任何方法来实现同样的目标?
我尝试了基于Ruby的File.open("C:\\Users\\testnode\\Downloads\\file_name"),但失败了,并显示以下错误消息:

./sample.rb:46:in `initialize': No such file or directory @ rb_sysopen - C:\Users\testnode\Downloads\file_name
 (Errno::ENOENT)
        from ./sample.rb:46:in `open'
        from ./sample.rb:46:in `<main>'
ca1c2owp

ca1c2owp1#

下载的文件位于远程机器上,而不是运行ruby代码的本地机器。要访问该文件,您需要使用ftp库或其他与网络相关的编程方法来访问机器以读取文件。

相关问题