Selenium - user000422/0 GitHub Wiki

導入

公式ドキュメント https://www.selenium.dev/ja/documentation 一般的にはアプリケーションサーバで完結させる。(クライアントのブラウザでキャプチャではない)

Selenium4.6からChromedriverの準備が不要になった! https://qiita.com/Chronos/items/7f56898af25523d04598

■Selenium IDE … 「Google Chrome」Seleniumブラウザ拡張機能

■Seleniumバージョン別動作環境 Pythonがメジャー

ver Java Python
Selenium3 Java8
Selenium4 Java11 Python3

■すべてクライアントで完結させる手順(実績があるのはこちら)

# ローカル Windowsマシン
Python3のインストール(Python3.11 ※2023/06)

Seleniumのインストール
コマンドプロンプト pip install selenium

Pytestのインストール
コマンドプロンプト pip install pytest

■すべてアプリケーションサーバで完結させる手順

# GoogleChrome
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

# ChromeDriver
# インストールしたGoogleChromeのバージョンと同じバージョンのChromeDriverをダウンロード
# バージョンの最初だけ一致させること(例:114
# URL https://chromedriver.chromium.org/downloads
cd /usr/local/src
wget https://chromedriver.storage.googleapis.com/114.0.5735.90/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
mv chromedriver /usr/local/bin/

# Python3(インストールされていない場合)
yum install python3

# Selenium Server
pip3 install selenium

# 日本語フォント
yum install ipa-gothic-fonts ipa-mincho-fonts ipa-pgothic-fonts ipa-pmincho-fonts

# composer
cd /usr/local/bin
curl -sS https://getcomposer.org/installer | php
# 拡張子を削除
mv /usr/local/bin/composer.phar /usr/local/bin/composer

開発

find_elements_by_*系は「Selenium4.3」で廃止された sample.py(テストスクリプト)

# from は 公式に準拠した記述
from selenium import webdriver
from selenium.webdriver.common.by import By

# ヘッドレスでの実行が必要になるため、ヘッドレスで必要なオプションを追加します。
options = webdriver.ChromeOptions()
options.add_argument('--headless') # ヘッドレスモード(画面描画を行わない)
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

# ChromeのWebDriverを作成
driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=options)

# ブラウザを起動(指定URL)
driver.get('https://google.com')

# スクリーンショット
# 引数:フルパス
driver.save_screenshot('sample.png')

# ブラウザを閉じる
driver.quit()

■基本操作(Selenium4)

# Selectを使用する際は必要
from selenium.webdriver.support.select import Select

# 待機 第一引数:秒
time.sleep(1)

# driver

# 要素の取得
sample_elment = driver.find_element(By.ID, "sample_id") # idから
sample_elment = driver.find_element(By.CLASS_NAME, "sample_class") # class名から
sample_elments = driver.find_elements(By.NAME, "sample_radio_btn") # name名から(ラジオボタン 配列)
sample_elment = driver.find_element(By.XPATH, '//input[@type="submit" and @value="検索"]') # 色んな要素の組み合わせから(idなどで特定できない場合の最終手段にしたい)

# クリック
sample_elment.click()
sample_elments[0].click() # 配列(ラジオボタンなど)

# テキスト入力(要素の取得と同時に行う)(一般的)
# 注意点 : すでに入力されている場合追記される
driver.find_element(By.NAME, "sample_class").send_keys("hello")

# セレクトボックス 特殊
sample_select_element = driver.find_element(By.NAME, "sample_select")
sample_select = Select(sample_select_element) # セレクトボックス用オブジェクトに変換
sample_select.select_by_index(0) # セレクトボックス指定(インデックス)

# 新しく開いたタブに切り替える(インデックス)
driver.switch_to.window(driver.window_handles[1])
# スマートフォン
  def setup_method(self, method):
    self.service = Service(executable_path=r"C:\WebDriver\bin\chromedriver.exe")
    self.options = Options()
    mobile_emulation = { "deviceName": "Galaxy S5" } # 端末指定
    self.options.add_experimental_option("mobileEmulation", mobile_emulation) # スマートフォン設定
    self.driver = webdriver.Chrome(service=self.service, options=self.options)
    self.vars = {}

Selenium IDE

エクスポートしたpythonファイルを実行する想定 起動コマンド(コマンドプロンプト) pytest C:\sample\export_test.py 起動コマンド(コマンドプロンプト) pytest -s C:\sample\export_test.py ※print関数で出力したい場合「s」オプション

Pytestルール ファイル名は「testXXX.py」 クラス名は「TestXXX」

# エクスポート後に追記したもの
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

# setup_methodの改造
  def setup_method(self, method):
    self.service = Service(executable_path='C:\WebDriver\bin\chromedriver.exe')
    self.options = Options()
    self.options.add_argument('--ignore-certificate-errors') ## ssl証明書が有効ではないサイト用(通常は必要ない)
    self.driver = webdriver.Chrome(service=self.service, options=self.options)
    self.vars = {}