LoginSignup
8
6

More than 5 years have passed since last update.

seleniumでmetaタグ出力メモ

Last updated at Posted at 2016-03-09

require 'selenium-webdriver'

# ブラウザ起動
driver = Selenium::WebDriver.for :firefox

# サイトマップページにアクセス
driver.navigate.to "https://www.example.com/sitemap"

# ページ内のリンク取得
elements = driver.find_elements(:tag_name, 'a')
links = elements.map{|a| a.attribute('href')}
links.uniq! #重複削除

links.each.with_index(1) do |url,i|

  sleep 2 #2秒待つ

  driver.navigate.to url
  puts url
  # title 出力
  puts driver.title
  # meta description 出力
  puts driver.find_element(:xpath, "//meta[@name='description']").attribute('content')
  # meta keywords 出力
  puts driver.find_element(:xpath, "//meta[@name='keywords']").attribute('content')

  # 画面キャプチャ
  #driver.save_screenshot('filename.png')

  # 100件で終了
  break if i == 100

end


# ブラウザ終了
driver.quit

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6