0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

webツールのID/パスワードを事前登録しておく

Posted at

社内のWebツール操作の自動化をするのに問題になるのが、ログインする際にID/パスワードを入力することです。
コード内にベタ書きするのは良くないですし、パスワードを変更するたびにコードを修正する必要性が生じます。

そこで、事前にID/パスワードを登録しておき、pythonプログラム実行時にID/パスワードを入力するツールを作ってみましたので、備忘録として残しておきます。

環境

  • OS : Windows 10 64bit
  • Python 3.6.0 (Anaconda3-4.3.1)

使用したモジュール

  • TKInter
  • csv
  • sys

コード

entry.py
import tkinter as tk
from tkinter import messagebox
import sys
import csv

# ID/PWDの登録
def Entry_ID_PW():
    System_id = str(Enter_ID.get())
    System_pw = str(Enter_PW.get())
    System_list = [System_id, System_pw]
    with open('System_entry.csv', 'w') as file:
        writer = csv.writer(file, lineterminator='')
        writer.writerow(System_list)

    # 結果をラベルに表示
    Label_Msg['text'] = "登録しました。終了してください"

# 処理の終了
def Entry_Finish():
    sys.exit()

# 操作ボックス
root = tk.Tk()
root.title(u"System ID/PW入力")
root.geometry("300x230")
Label_Msg = tk.Label(root, text=u'SystemのID/PWDを入力してください。')
Label_Msg.pack()
# ID
Label_ID = tk.Label(root, text=u'System ID')
Label_ID.pack()
Enter_ID = tk.Entry(root)
Enter_ID.insert(tk.END, '')
Enter_ID.pack()
# PW
Label_PW = tk.Label(root, text=u'System Password')
Label_PW.pack()
Enter_PW = tk.Entry(root)
Enter_PW.insert(tk.END, '')
Enter_PW.pack()
# 登録ボタン
Label_Blanc = tk.Label(root, text=u'')
Label_Blanc.pack()
Entry_Btn = tk.Button(root, text=u'登録')
Entry_Btn["command"] = Entry_ID_PW
Entry_Btn.pack()
# 終了ボタン
Label_Blanc = tk.Label(root, text=u'')
Label_Blanc.pack()
Finish_Btn = tk.Button(root, text=u'終了')
Finish_Btn["command"] = Entry_Finish
Finish_Btn.pack()

root.mainloop()

説明

入力ボックス

入力ボックスを表示して、ID/パスワードを入力させます。
入力ボックスの説明は、こちらの記事(メニューボックスからプログラムを実行する)を参照してください。
entry.PNG

CSVファイルへのID/パスワードの保存

入力したID/パスワードはCSVファイルに保存します。

entry.py
# ID/PWDの登録
def Entry_ID_PW():
    System_id = str(Enter_ID.get())
    System_pw = str(Enter_PW.get())
    System_list = [System_id, System_pw]
    with open('System_entry.csv', 'w') as file:
        writer = csv.writer(file, lineterminator='')
        writer.writerow(System_list)

入力したID/パスワードは、文字列(string)としてリスト化。
それをSystem_entry.csvというCSVファイルに保存します。

CSVファイルはpythonファイルと同フォルダに作成されます。
別のフォルダに保存したい場合はパスを指定して保存してください。
(例)C:\document\System_entry.csv

lineterminatorはwriterが作り出す各行を終端する際に用いられる文字列で、デフォルトでは\r\nです。
ID/パスワードを取り出す時に改行コードが入ってしまうと、それを取り除く作業が必要になるため、''とします。

writerowは、Writerオブジェクトに使うことで、csvファイルへの一行の書き込みができます。
writerowメソッドの引数はリスト(ここではSystem_list)です。

CSVファイルからのID/パスワードの取り出し

Webツールを自動操作するためのpythonファイルに以下のコードを記載します。

web_automation.py
# pfosログイン情報を取得し、リスト化する。
f = open("W:\python\System_entry.csv", "r")
reader = csv.reader(f)
data = [ e for e in reader ]
f.close()

# ログイン情報をリストから取り出す。
System_id = data[0][0]
System_pw = data[0][1]

これでCSVファイルからID/パスワードを取り出すことができますので、
その後、selenium等を使い、WebツールにID/パスワードを入力し、ログインさせて上げればOKです。

備考

なお、SystemのログインID/パスワードを変更した場合は、前述の入力ボックスに新しいID/パスワードを再登録すれば、CSVファイルに上書き保存されるようになっています。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?