#author("2020-03-31T18:00:51+09:00","default:yakumo_murakami","yakumo_murakami")
[[Ruby]]

* コード [#x5783a85]

こんな感じです。

 require "selenium-webdriver"
 
 options = Selenium::WebDriver::Firefox::Options.new(args: ["--headless"])
 driver = Selenium::WebDriver.for :firefox, options: options
 driver.navigate.to "https://www.yahoo.co.jp"
 puts driver.title
 driver.save_screenshot("screenshot.png")
 File.write("index.html",driver.page_source)

* クラス定義 [#a0a5e95e]

もう少しruby的にしてみましょうか?

 require "selenium-webdriver"
 
 class Web
  def initialize(url)
    @url = url.clone
    @ng = nil
    begin
      urlget
    rescue => ex
      @ng = ex
    end
  end
  def urlget
    options = Selenium::WebDriver::Firefox::Options.new(args: ["--headless"])
    @driver = Selenium::WebDriver.for :firefox, options: options
    @driver.navigate.to @url
    @title = @driver.title
  end
  def title
    @title
  end
  def screenshot(fn)
    @driver.save_screenshot(fn)
  end
  def write(fn)
    File.write(fn,@driver.page_source)
  end
 end
 
 ############
 
 c = Web.new("https://www.yahoo.co.jp")
 p c.title
 c.screenshot("yahoo.png")
 c.write("yahoo.html")


こんな感じかな?

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS