Seleium [SBI証券]自動ログイン.py

[source]はディレクトリ、↓はディレクトリ内の構成例

[source]  / 00_xxx.vbs / 01_xxx.vbs

      ↓

00_xxx.bat / 00_xxx.py / 01_xxx.py

あらかじめ こちら を参照してPythonのインストール必須


Chromeや他のブラウザをSeleniumで動かすにはブラウザとは別にそのブラウザのバージョンに応じた、"WebDriver"の用意が必要です。

ただ昨今のWebブラウザは頻繁に更新が行われバージョンが刷新されます。
これではその度に"WebDriver"のダウンロードも必要になってきます。(面倒)

そのわずらわしさから解放してくれるのが"webdrive_manager"ライブラリです。
このライブラリはプログラム実行時に自動でブラウザのバージョンを判別し、適切な"WebDriver"をダウンロードし、使用してくれます。

 

  00_xxx.py


#Python version : 3.11.4

#Windows 11 Pro
#Google Chrome : 117.0.5938.150

from selenium.webdriver.chrome import service
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By


# Chromeを起動するためのオプションに拡張機能を追加する
chrome_options = webdriver.ChromeOptions()
#00_xxx.batで起動したchromeのポートを60100で指定しているため必要なオプション
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:60100")

service = Service(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.implicitly_wait(10) # seconds
driver.get('https://www.sbisec.co.jp/ETGate/')

#<input type="text" name="user_id" value="" onkeypress="enterpress();" onfocus="Monc(&quot;UI&quot;)" data-gtm-form-interact-field-id="0">
driver.find_element(By.NAME,'user_id').send_keys("あなたのID")
#<input type="password" name="user_password" maxlength="20" value="" onkeypress="enterpress();" onfocus="Monc(&quot;PW&quot;)" data-gtm-form-interact-field-id="1">
driver.find_element(By.NAME,'user_password').send_keys("あなたのPW")
#<input type="submit" name="ACT_login" value="ログイン" onclick="changeNWEBFLG();">
driver.find_element(By.NAME,'ACT_login').click()

 

あなたのID/PW 部分につきましては自分用に置換をお願いします。
00_xxx.vbsの作成
00_xxx.batの作成