LoginSignup
2
0

More than 5 years have passed since last update.

Firefox の Headless モード (Ruby)

Posted at

Firefox の Headless モードの使い方です。

headless_firefox.rb
#! /usr/bin/ruby
# -*- coding: utf-8 -*-
#
#   headless_firefox.rb
#
#                   Sep/18/2018
#
# ------------------------------------------------------------------
require 'selenium-webdriver'

# ------------------------------------------------------------------
STDERR.puts "*** 開始 ***"
url_target = ARGV[0]
STDERR.puts url_target

options = Selenium::WebDriver::Firefox::Options.new(args: ['-headless'])

driver = Selenium::WebDriver.for(:firefox, options: options)

driver.get(url_target)

version = driver.execute_script('return jQuery.fn.jquery')

STDERR.puts "Using jQuery #{version}"

puts driver.title

driver.save_screenshot("out.png")

html = driver.page_source

driver.quit

print(html)

STDERR.puts "*** 終了 ***"
#
# ------------------------------------------------------------------

実行結果

$ ./headless_firefox.rb https://ekzemplaro.org/storytelling/
*** 開始 ***
https://ekzemplaro.org/storytelling/
Using jQuery 3.3.1
ストーリーテリング
<html lang="ja"><head>
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8">
<script src="/js/jquery-3.3.1.min.js"></script>
<script src="storytelling.js"></script>
<link rel="stylesheet" href="storytelling.css">
<title>ストーリーテリング</title>
</head>
<body>
<h2>ストーリーテリングを味わう機会</h2>
<blockquote>
<div class="information"></div>
</blockquote>
<h2>ストーリーテリングの私のレパートリー</h2><p>
</p><div class="contents"></div>

<hr>
<a href="html_src/">src</a><br>
<hr>
<a href="../">Return</a><p>
Aug/18/2018 AM 07:00</p><p>


</p></body></html>*** 終了 ***
2
0
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
2
0