简体   繁体   English

如何在 Watir 中保存网页?

[英]How can I save a web page in Watir?

Using Ruby and Watir , can I save a web page the same way as doing a right mouse-click and " save page with name "?使用RubyWatir ,我可以像右键mouse-click并“ save page with name ”一样save page with name吗?

I need to save the current web page from a script.我需要从脚本中保存当前网页。

Yes, you can do it with watir .是的,你可以用watir做到这watir Just open a page and save the browser.html to any destination you want:只需打开一个页面并将browser.html保存到您想要的任何目的地:

b = Watir::Browser.new :phantomjs    # I am using phantomjs for scripted browsing
b.goto 'http://google.com'
File.open('/tmp/google', 'w') {|f| f.write b.html }

I don't know about watir, but I know the way to do it using Selenium Web Driver is using the page source method.我不知道 watir,但我知道使用 Selenium Web Driver 的方法是使用页面源方法。 Check out the docs for that here: http://selenium.googlecode.com/git/docs/api/rb/Selenium/WebDriver/Driver.html#page_source-instance_method在此处查看文档: http : //selenium.googlecode.com/git/docs/api/rb/Selenium/WebDriver/Driver.html#page_source-instance_method

using this, you should get the whole source.使用这个,你应该得到整个源。

You can now save the source, by just creating a new file.您现在可以通过创建一个新文件来保存源。 I haven't tried this but you can check it out.我没有试过这个,但你可以看看。

driver = Selenium::WebDriver.for(:firefox)
driver.get(URL_of_page_to_save)
file = File.new(filename, "w")
file.puts(driver.page_source)
file.close

Not sure if this saves all elements of the page.不确定这是否会保存页面的所有元素。

Hope this helped a bit!希望这有点帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM