0
0

More than 1 year has passed since last update.

【Python覚書】selenium on raspberry pi(002)_環境構築~画面キャプチャまで

Last updated at Posted at 2021-12-31

はじめに

以前、IoTの勉強をしようと思って、raspberry pi 3 modelBを数個購入したのですが、長い間眠っている状態でした。

これを機に、稼働させてみようと思い、「【Python覚書】selenium on centos stream 8(001)」とほぼ同じ内容ですが、raspberry piのOSである「Buster版」上での環境構築手順を記載しようと思います。

OSの最新化

# パッケージリストの更新
sudo apt-get -y update
# パッケージの更新
sudo apt-get -y upgrade
# OSのアップグレード
sudo apt-get -y dist-upgrade

python環境構築(設定変更)

raspberry piのOSは初期状態でpython2とpython3がインストール済みなので、まずはpython3をデフォルトに設定します。

# 設定前のバージョン確認
python --version

cd /usr/bin
# Python 2のシンボリックリンクを解除
sudo unlink python
# Python 3.7へのシンボリックリンクに変更
sudo ln -s python3 python

# 設定後のバージョン確認
python --version

seleniumのインストール

pip3 install --upgrade pip
pip3 install --upgrade setuptools
pip3 install selenium

Chromiumドライバのインストール

sudo apt install -y chromium-chromedriver

which chromedriver

pythonプログラムの動作確認

Action_configurate.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome import service as fs

from time import sleep

options = Options()
options.add_argument('--headless')
options.add_argument("--no-sandbox")

# ドライバー指定でChromeブラウザを開く
chrome_service = fs.Service(executable_path="/usr/bin/chromedriver")  # 修正箇所
driver = webdriver.Chrome(service=chrome_service, options=options)  # 修正箇所

# ウィンドウサイズを設定
driver.set_window_size(1280, 720)

# 対象のページを指定
driver.get('https://qiita.com/o_chan_t')
sleep(5)

# キャプチャするページの幅
my_width = driver.execute_script('return document.body.scrollWidth')
# キャプチャするページの高さ
my_height = driver.execute_script('return document.body.scrollHeight')
# 保存するウィンドウサイズ(画像サイズ)
driver.set_window_size(my_width, my_height)

driver.save_screenshot('Qiita_MyPage_Capture.png')

raspberry piの方は、日本語の文字化け問題が起きませんでした。

0
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
0
0