【问题标题】:Watir - How to save a pdf file when the file opens in htmlWatir - 当文件以html打开时如何保存pdf文件
【发布时间】:2014-07-06 16:45:08
【问题描述】:

当我点击一个链接时,pdf 在浏览器中打开。我的测试要求我将 pdf 保存到桌面上。

我能够找到打开 pdf 但无法保存的新窗口。至于保存 pdf,我必须将鼠标悬停在底部以获取 pdf 图标,或者我必须转到文件 - > 另存为以保存 pdf。

【问题讨论】:

    标签: html pdf browser save watir


    【解决方案1】:

    这里是 sample code 在 Firefox 中如何做到这一点

    download_directory = "#{Dir.pwd}/downloads"
    download_directory.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows?
    
    profile = Selenium::WebDriver::Firefox::Profile.new
    profile['browser.download.folderList'] = 2 # custom location
    profile['browser.download.dir'] = download_directory
    profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf"
    
    b = Watir::Browser.new :firefox, :profile => profile
    

    和铬

    download_directory = "#{Dir.pwd}/downloads"
    download_directory.gsub!("/", "\\") if  Selenium::WebDriver::Platform.windows?
    
    profile = Selenium::WebDriver::Chrome::Profile.new
    profile['download.prompt_for_download'] = false
    profile['download.default_directory'] = download_directory
    
    b = Watir::Browser.new :chrome, :profile => profile
    

    使用 ie 你可能不得不做这样的事情:

    f = open('sample.pdf')
    begin
        http.request_get('/sample.pdf') do |resp|
            resp.read_body do |segment|
                f.write(segment)
            end
        end
    ensure
        f.close()
    end
    

    Code Ref

    【讨论】:

    • IE 浏览器需要这个。如果有什么办法,请告诉我。
    • 我尝试使用 File -> Save AS 工具栏保存 pdf。所以发送密钥来点击文件 -> 另存为
    • @browser.send_keys([:control,:shift,'s']) --Control+Shift+S
    • @browser.send_keys([:alt,'fa']) --Alt+F+A 键
    猜你喜欢
    • 2012-09-25
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    相关资源
    最近更新 更多