Watir 不会下载 PDF,只能在查看器中打开 [英] Watir won't download PDF, only opens in viewer

查看:61
本文介绍了Watir 不会下载 PDF,只能在查看器中打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用 Selenium 进行测试,但无法下载 pdf,pdf 文件一直打开.

I was trying to test with Selenium, but can not download a pdf, pdfs keep opening.

见我的另一篇文章:RUBY:Selenium webdriver,设置下载pdf文件而不是打开它们

有人建议尝试 Watir,所以我尝试了,并且得到了相同的结果.这是我的 Watir 设置.请告知如何解决此问题.

It was advised to try Watir, so I did, and I get the same result. Here is my Watir setup. Please advise on how to fix this issue.

require 'watir'
require 'pry'

prefs = {
  download: {
    prompt_for_download: false,
    default_directory: '/Users/ar/pdf_downloads'
  }
}

browser = Watir::Browser.new :chrome, options: {prefs: prefs}

# Goto Login Page (file)
url="file:///Users/ar/info.html"
browser.goto url

browser.button(id: 'formsubmit').click

sleep 5
# Goto info
info_url = 'https://webapp.domain.com/info'
browser.goto info_url

sleep 5
elements = browser.elements(css: "#ar-pdfreport a")
link = elements.first.attribute("href")
browser.goto link

推荐答案

Selenium-WebDriver v3.142.7 中存在一个错误,即在首选项中使用符号会产生错误的结果 - 例如未设置下载目录.请参阅 https://github.com/SeleniumHQ/selenium/issues/7917 了解更多信息详情.

There is a bug in Selenium-WebDriver v3.142.7 where using symbols for the prefs generates the wrong result - eg does not set the download directory. See https://github.com/SeleniumHQ/selenium/issues/7917 for more details.

将符号切换为字符串:

prefs = {
  download: {
    'prompt_for_download' => false,
    'default_directory' => '/Users/ar/pdf_downloads'
  },
  plugins: {
    'always_open_pdf_externally' => true
  }  
}   

这篇关于Watir 不会下载 PDF,只能在查看器中打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆