0
2

More than 1 year has passed since last update.

SeleniumでSlackに自動でログインする

Last updated at Posted at 2022-01-28

ブラウザの操作を自動化するライブラリ「Selenium」を使ってSlackのログインを自動化するプログラムの一例です。

auto-login.py
# (Selenium 4.1.0 で動作確認済み)
# webdriver は使用しているChromeのバージョンに合わせて以下のページから取得する。
# https://chromedriver.chromium.org/downloads

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome import service as fs
import time

# parameters
slackurl    = 'sample.slack.com'        # Slack URL
mailaddress = 'sample@gmail.com'        # 登録済みメールアドレス
mypassword  = 'abrakadabra'             # ログインパスワード
waittime    = 10.0                      # (sec.) 通信速度 & PCスペックに応じて調整
logintime   = 100.0                     # (sec.) 適宜調整
driverpath  = 'C:/Users/[user名]/Downloads/chromedriver_win32/chromedriver.exe' # webdriver が置かれている path を指定
url         = 'https://app.slack.com/client/XXXYYYZZZ/AAABBBCCC'                # 目的地となる Slack WorkSpace のURL


try:
    # Webページにアクセス
    chrome_service = fs.Service(executable_path=driverpath)
    driver = webdriver.Chrome(service=chrome_service)
    driver.maximize_window()  #全画面表示にする (オプション)
    driver.get(url)
    driver.implicitly_wait(waittime)  # 遷移待ち

    # Slack WorkSpace の選択
    ch_element = driver.find_element(By.ID, 'domain')
    ch_element.send_keys(slackurl)
    ch_element.send_keys(Keys.ENTER)
    driver.implicitly_wait(waittime)  # 遷移待ち

    # 自動ログイン
    mail_element = driver.find_element(By.ID, 'email')
    mail_element.send_keys(mailaddress)
    password_element = driver.find_element(By.ID, 'password')
    password_element.send_keys(mypassword)
    password_element.send_keys(Keys.ENTER)
    driver.implicitly_wait(waittime)  # 遷移待ち

    # 自動ログインに成功
    print('Successfully logged in:')
    url     = driver.current_url    # URLを取得する (オプション)
    title   = driver.title          # タイトルを取得する (オプション)
    print(url, title)

    # 任意時間ログイン状態を維持
    time.sleep(logintime)

finally:
    # セッションの終了
    driver.quit()
    print("Done.")

セキュリティの問題が生じない範囲で適宜改変してご利用下さい。

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