【初心者でも簡単】Selenium×Pythonで実現するWeb自動化:Office365にアクセス&ログイン処理の実践的なサンプルコードと解説

Webブラウザを用いた操作の自動化は、さまざまな業務を効率化するために不可欠。
例:定期的に更新されるデータの収集や、指定のサイトからファイルを自動的にダウンロード…etc

本記事では、PythonSeleniumを組み合わせて、Office365にアクセス、ログイン処理までの捜査を自動化する方法について解説する

必要なライブラリのインストールとインポート

まず、必要なライブラリをインストールし、インポートする必要がある。以下のライブラリを使用する。

  • os
  • time
  • selenium
  • webdriver_manager
  • pytesseract

インポートのコードは以下の通りである。

import os
import time
from selenium import webdriver
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.microsoft import EdgeChromiumDriverManager
import pytesseract

メールアドレス、ユーザーID、パスワードの設定

自動ログインに必要な情報を変数として設定する サンプルとして、記載している為、実際に使用する際は、個々人のログイン情報を変数に設定するように修正

# メールアドレス
email_address = "example@example.com"

# ユーザーID
user_id = "example_user"

# パスワード
user_password = "example_password"

WebDriverの初期設定

Edgeブラウザを使用するための設定を行う。以下のコードは、EdgeのWebDriverを初期化し、必要なオプションを設定している。

# Tesseract OCRのパスを設定
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

# Edgeブラウザの設定
options = webdriver.EdgeOptions()
options.use_chromium = True

# WebDriverの初期化
service = EdgeService(EdgeChromiumDriverManager().install())
driver = webdriver.Edge(service=service)

ログイン関数の定義

ログインを自動化するための関数loginを定義する。この関数では、指定したメールアドレス、ユーザーID、パスワードを用いてログインを試みる。

def login():
    try:
        # メールアドレスが表示されているdiv要素をクリック
        email_div_xpath = f"//div[contains(text(), '{email_address}')]"
        email_div = WebDriverWait(driver, 30).until(
            EC.element_to_be_clickable((By.XPATH, email_div_xpath))
        )
        time.sleep(15)
        email_div.click()
        time.sleep(5)

        # ユーザーID入力
        username_field = driver.find_element(By.ID, "username")
        username_field.click()
        username_field.send_keys(user_id)

        # パスワード入力
        password_field = driver.find_element(By.ID, "password")
        password_field.click()
        password_field.send_keys(user_password)

        # ログインボタン押下
        submit_button = driver.find_element(By.ID, "submit")
        submit_button.click()

        time.sleep(5)

        # サインインの状態を保持するか確認
        stay_signed_in_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.ID, "idSIButton9"))
        )
        stay_signed_in_button.click()

        time.sleep(5)

    except Exception as e:
        print(f"An error occurred during login: {e}")
        driver.quit()

自動操作の実行

次に、指定したURLにアクセスし、必要に応じてログインを行い、特定のボタンをクリックする処理を実行する。

try:
    # Microsoft Officeのページに遷移
    office_url = "https://www.office.com/"
    driver.get(office_url)

    driver.maximize_window()

    # 10秒待機
    time.sleep(10)

    # ログインが必要か確認し、必要であればログイン
    if "login" in driver.current_url:
        login()

    # ページが完全に読み込まれるまで待機
    WebDriverWait(driver, 20).until(
        EC.presence_of_element_located((By.XPATH, "//title[contains(text(), 'Office')]"))
    )
    time.sleep(3)

    # "aria-label"に「ダウンロード」を含む要素を特定してクリック
    download_button_xpath = "//button[contains(@class, 'css-') and contains(@aria-label, 'ダウンロード')]"
    download_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, download_button_xpath))
    )
    download_button.click()
    # DL待機
    time.sleep(15)
finally:
    # ブラウザを閉じる
    driver.quit()