我只是想知道是否有一种优雅的方式将浏览器内容(在我的情况下是一个pdf文件)保存到磁盘上,使用Watir自动浏览器(导航到页面,然后“点击”指向pdf文档的链接)?我希望在浏览器对象上执行“send_keys”,但它似乎不起作用 - browser.send_keys("+^S")
。我可以使用AutoIt,但这要求我首先找到一个窗口(或者我错了?)并且我发现这种方法不太可靠(如果碰巧有重复的窗口......)
这是代码的样子......
b=Watir::Browser.new
b.goto "http://somesite.com/somepage.htm" #assume this page contains a bunch of links
b.link(:text, /pdf/i).when_present.click #this action loads the pdf in the browser
b.send_keys("+^s") #try to invoke acrobat's "save as" <-- does NOT work!
最后一行可能不起作用,因为Acrobat控件可能无法清晰对焦,可能无法接收预定的击键...
我知道Zeljko指出有几种选择。但我想知道为什么我无法访问基础文档流?
答案 0 :(得分:1)
答案 1 :(得分:0)
我成功地将Ctrl-S发送到浏览器(使用firefox测试):
b.send_keys([:control, 's'])
但它确实无济于事。在此之后,您将进入“另存为” - 对话。 但是你不会保存PDF,而是整个网站(可能取决于你的浏览器设置)。
要将此保存控制为对话,我使用了AutoIt:
require 'win32ole' #For AutoIt
au3 = WIN32OLE.new("AutoItX3.Control")
wintitle = "Speichern unter" #<-- adapt language specific text
download_directory = File.join(Dir.pwd, "downloads")
win_exists = au3.WinWait(wintitle, nil, 5)
if (win_exists > 0)
au3.WinActivate(wintitle)
au3.Send('!n') #Dateiname
au3.Send(download_directory.gsub(/\//, '\\'))
sleep 1
au3.Send('{ENTER}')
end
现在我有一个包含我的pdf的目录,我可以这样做:
pdf_mask = "%s-Dateien/*.pdf" % download_directory #<- Again language specific
Dir[pdf_mask].each{|pdf|
#do something twith the file
}
我是AutoIt的新手,也许我找到了一种更简单的方法。
但我同意另一个答案:
最简单的解决方案是禁用弹出窗口并自动下载文件。