Created
August 1, 2018 08:18
-
-
Save aquarla/80a45feab28a365323f55a0905fdc4b5 to your computer and use it in GitHub Desktop.
Selenium WebDriver + Headless (Firefox|Chrome) でマストドンの個別ページのスクリーンショットを取るヤツ(要ImageMagick)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'selenium-webdriver' | |
mode = "firefox" | |
if mode == "firefox" | |
options = Selenium::WebDriver::Firefox::Options.new | |
options.add_argument('-headless') | |
profile = Selenium::WebDriver::Firefox::Profile.new | |
profile["intl.accept_languages"] = "ja,en-us,en" | |
options.profile = profile | |
driver = Selenium::WebDriver.for :firefox, options: options | |
elsif mode == "chrome" | |
options = Selenium::WebDriver::Chrome::Options.new | |
options.add_argument('--headless') | |
driver = Selenium::WebDriver.for :chrome, options: options | |
end | |
driver.get "https://iwatedon.net/@aquarla" | |
height = driver.execute_script("return window.outerHeight - window.innerHeight + document.body.scrollHeight") | |
driver.manage.window.resize_to(1024, height) | |
driver.save_screenshot "./screenshots/whole.png" | |
elements = driver.find_elements(:css => '.entry') | |
elements.each_with_index do |element, i| | |
`convert ./screenshots/whole.png -crop #{element.size.width}x#{element.size.height}+#{element.location.x}+#{element.location.y} ./screenshots/#{sprintf("%02d", i)}.png` | |
end | |
driver.quit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment