IPO自動申込

Python

失敗

・ログイン処理の直後に、SBIのページが「いつもと異なるデバイスが検出されました」というページに切り替わり、「メールを送信する」ボタンが出てきたので、2段階認証しないとログインできなかった

・自動ログインだとなぜバレたのかよくわからない

Code

find_element_by_xpathは古いのでfind_element(By.XPATH, ‘//input[@name=”xxx”]’)を使うこと

from selenium import webdriver
import chromedriver_binary
# chromedriverを自動更新するためのモジュール
# pip install chromedriver_autoinstaller
import chromedriver_autoinstaller
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

from time import sleep
import datetime
import requests
import re
import os
import shutil

# Chromeのバージョンに合ったdriverを自動インストール
chromedriver_autoinstaller.install()

driver = webdriver.Chrome()

# headless mode
# options = Options()
# options.add_argument('--headless')
# driver = webdriver.Chrome(options=options)

URL_short = 'https://www.sbisec.co.jp'
URL = 'https://www.sbisec.co.jp/ETGate'

USERNAME = 'あなたのユーザー名'
PASSWORD = 'あなたのログインパスワード'
ORDER_NUM = '200(申し込みたい株数)'
ORDER_PASS = 'あなたの取引パスワード'

driver.get(URL)
sleep(8)

# ログイン処理
flag = False
try:
    # ユーザー名の入力
    username_input = driver.find_element(By.XPATH, '//input[@name="user_id"]')
    username_input.send_keys(USERNAME)
    sleep(1)

    # パスワードの入力
    password_input = driver.find_element(By.XPATH, '//input[@name="user_password"]')
    password_input.send_keys(PASSWORD)
    sleep(1)

    # ログインボタンの押下
    login_button = driver.find_element(By.XPATH, '//input[@name="ACT_login"]')
    login_button.submit()
    sleep(5)

except Exception:
    flag = True
    print('ログイン処理に失敗しました')


# IPO一覧ページ
# https://m.sbisec.co.jp/switchnaviMain ???
IPO_LIST_URL = 'https://m.sbisec.co.jp/oeliw011?type=21'
if flag is False:
    try:
        driver.get(IPO_LIST_URL)
        sleep(5)
    except Exception:
        flag = True
        print('IPO一覧画面に遷移できませんでした')


# IPO一覧ページからIPO個別銘柄ページに移動
if flag is False:
    try:
        ipo_button = driver.find_element(By.XPATH, '//img[@alt="申込"]/parent::a')
        ipo_button.click()
        sleep(5)
    except Exception:
        flag = True
        print('IPO個別銘柄ページに遷移できませんでした')


# IPO個別銘柄ページで情報入力
if flag is False:
    try:
        order_num = driver.find_element(By.XPATH, '//input[@name="suryo"]')
        order_num.send_keys(ORDER_NUM)
        sleep(1)

        order_price = driver.find_element(By.XPATH, '//input[@id="strPriceRadio"]')
        order_price.click()
        sleep(1)

        order_pass = driver.find_element(By.XPATH, '//input[@name="tr_pass"]')
        order_pass.send_keys(ORDER_PASS)
        sleep(1)

        order_button = driver.find_element(By.XPATH, '//input[@type="submit"]')
        order_button.submit()
    except Exception:
        flag = True
        print('IPO個別銘柄ページで情報入力に失敗しました')

# IPO申込確認ページ
if flag is False:
    try:
        ipo_name = driver.find_element_by_xpath('//font[@class="lbody"]/child::b')
        ipo_name = ipo_name.text

        order_button_fin = driver.find_element_by_xpath('//input[@name="order_btn"]')
        order_button_fin.click()
    except Exception:
        flag = True
        print('申込確認時にエラーが発生しました')

# 
BACK