[英]How can I save a web page in Watir?
使用Ruby
和Watir
,我可以像右鍵mouse-click
並“ save page with name
”一樣save page with name
嗎?
我需要從腳本中保存當前網頁。
是的,你可以用watir
做到這watir
。 只需打開一個頁面並將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 }
我不知道 watir,但我知道使用 Selenium Web Driver 的方法是使用頁面源方法。 在此處查看文檔: http : //selenium.googlecode.com/git/docs/api/rb/Selenium/WebDriver/Driver.html#page_source-instance_method
使用這個,你應該得到整個源。
您現在可以通過創建一個新文件來保存源。 我沒有試過這個,但你可以看看。
driver = Selenium::WebDriver.for(:firefox)
driver.get(URL_of_page_to_save)
file = File.new(filename, "w")
file.puts(driver.page_source)
file.close
不確定這是否會保存頁面的所有元素。
希望這有點幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.