Selenium マウスとキーボード操作

Selenium は豊富なマウス操作とキーボード操作機能を提供し、クリック、ダブルクリック、ドラッグ&ドロップ、ホバー、テキスト入力、コンビネーションキーの押下など、ユーザーの操作をシミュレートできます。

以下は、Selenium でよく使用されるマウス操作とキーボード操作、およびその説明です:

マウス操作

操作タイプメソッド説明
クリックclick(element) 或 click()指定した要素または現在のマウス位置をクリックします。
ダブルクリックdouble_click(element) 或 double_click()指定した要素または現在のマウス位置をダブルクリックします。
右クリックcontext_click(element) 或 context_click()指定した要素または現在のマウス位置を右クリックします。
ドラッグ&ドロップdrag_and_drop(source, target)ソース要素をターゲット要素にドラッグ&ドロップします。
ホバーmove_to_element(element)マウスを指定した要素の上に移動します。
押下と解放click_and_hold(element)とrelease()指定した要素を押したまま離します。ドラッグ操作を実現するために使用します。

キーボード操作

操作タイプメソッド説明
テキスト入力send_keys("text")入力フィールドまたはテキストエリアに指定したテキストを入力します。
コンビネーションキーを押すsend_keys(Keys.CONTROL + 'a')コンビネーションキー(例:Ctrl + A)の押下をシミュレートします。
単一キーを押すsend_keys(Keys.KEY_NAME)単一のキー(例:Enter、Tab、Shift など)の押下をシミュレートします。
キーを離すkey_up(key)押したキーを離します。

よく使うキーボードのキー値

キー値説明
Keys.ENTEREnterキー
Keys.TABTabキー
Keys.SHIFTShiftキー
Keys.CONTROLCtrlキー
Keys.ALTAltキー
Keys.ESCAPEEscキー
Keys.BACKSPACEBackspaceキー
Keys.SPACEスペースキー
Keys.ARROW_UP上矢印キー
Keys.ARROW_DOWN下矢印キー
Keys.ARROW_LEFT左矢印キー
Keys.ARROW_RIGHT右矢印キー

マウス操作(ActionChains クラス)

Selenium では、マウス操作は主にActionChainsクラスを使用して実現します。ActionChainsクラスを使用すると、クリック、ダブルクリック、右クリック、ドラッグ&ドロップ、ホバーなどの複雑なマウス操作を実行できます。

クリック、ダブルクリック、右クリック

  1. クリック(Click):マウスの左クリック操作をシミュレートします。
  2. ダブルクリック(Double Click):マウスの左ボタンによるダブルクリック操作をシミュレートします。
  3. 右クリック(Right Click):マウスの右クリック操作をシミュレートします。

例

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

# 正しいドライバーパスを設定
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)


# ウェブページを開く
driver.get("https://example.com")

# 要素を特定する
element = driver.find_element(By.ID, "element_id")

# ActionChains オブジェクトを作成
actions = ActionChains(driver)

# クリック操作
actions.click(element).perform()

# ダブルクリック操作
actions.double_click(element).perform()

# 右クリック操作
actions.context_click(element).perform()

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

ドラッグ&ドロップ操作

ドラッグ&ドロップ操作とは、ある要素を別の位置または別の要素の上にドラッグすることです。

例

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

# 正しいドライバーパスを設定
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)


# ウェブページを開く
driver.get("https://example.com")

# ドラッグする要素とターゲット要素を特定する
source_element = driver.find_element(By.ID, "source_element_id")
target_element = driver.find_element(By.ID, "target_element_id")

# ActionChains オブジェクトを作成
actions = ActionChains(driver)

# ドラッグ&ドロップ操作
actions.drag_and_drop(source_element, target_element).perform()

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

ホバー操作

ホバー操作とは、マウスポインターを特定の要素の上に置く操作で、通常はドロップダウンメニューを表示したり、ツールチップを表示したりするために使用されます。

例

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

# ブラウザドライバーを初期化
driver = webdriver.Chrome()

# ウェブページを開く
driver.get("https://example.com")

# 要素を特定する
element = driver.find_element(By.ID, "element_id")

# ActionChains オブジェクトを作成
actions = ActionChains(driver)

# ホバー操作
actions.move_to_element(element).perform()

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

キーボード操作(Keys クラス)

Selenium のキーボード操作は主にKeysクラスを使用して実現します。Keysクラスは、コンビネーションキーの送信や特殊キー操作など、さまざまなキーボードキーのシミュレーション操作を提供します。

コンビネーションキーの送信

コンビネーションキー操作とは、複数のキーを同時に押すことで、例えばCtrl + C(コピー)またはCtrl + V(貼り付け)です。

例

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

# 正しいドライバーパスを設定
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)

# ウェブページを開く
driver.get("https://example.com")

# 入力フィールドを特定する
input_element = driver.find_element(By.ID, "input_element_id")

# 内容を入力してコンビネーションキーを送信
input_element.send_keys("Hello, World!")
input_element.send_keys(Keys.CONTROL, 'a')  # すべて選択
input_element.send_keys(Keys.CONTROL, 'c')  # コピー
input_element.send_keys(Keys.CONTROL, 'v')  # 貼り付け

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

特殊キー操作(Enter、Tab、Shift など)

特殊キー操作とは、キーボード上の特殊キーの押下をシミュレートする操作です。例えばEnter、Tab、Shiftなどです。

例

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

# 正しいドライバーパスを設定
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)

# ウェブページを開く
driver.get("https://example.com")

# 入力フィールドを特定する
input_element = driver.find_element(By.ID, "input_element_id")

# 内容を入力して Enter キーを押す
input_element.send_keys("Hello, World!")
input_element.send_keys(Keys.ENTER)

# Tab キーを押して次の要素に切り替える
input_element.send_keys(Keys.TAB)

# Shift キーを押しながら大文字を入力
input_element.send_keys(Keys.SHIFT, "a")  # 大文字の 'A' を入力

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

総合サンプル

以下は、マウスとキーボード操作の使用方法を示す完全なサンプルです:

例

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

# 正しいドライバーパスを設定
service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)

# ウェブページを開く
driver.get("https://www.example.com")

# マウス操作
element = driver.find_element(By.ID, "button")
actions = ActionChains(driver)
actions.move_to_element(element).click().perform()

# キーボード操作
input_box = driver.find_element(By.ID, "input-box")
input_box.send_keys("Hello, Selenium!")
input_box.send_keys(Keys.ENTER)

# ブラウザを閉じる
driver.quit()
その他の拡張