0
0

03faxのサイトから請求明細を保存する

Last updated at Posted at 2023-12-08

インボイス制度導入に伴い、使用しているFAXサービス(03fax)の請求明細をダウンロードして保存する作業を自動化したいと思い、Seleniumを使って以下のコードを書きました。

03fax.py
from time import sleep

from selenium.webdriver.common.by import By

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService

#Seleniumで使用するドライバのバージョンアップ自動化
from webdriver_manager.chrome import ChromeDriverManager

# Seleniumの初期化
def initialize():
   new_driver = ChromeDriverManager().install()
   service  = ChromeService(executable_path=new_driver)
   
   options = webdriver.ChromeOptions()
   options.add_argument('--headless')
   #保存先の指定
   prefs = {
       "download.default_directory": 'xxxxxxx',
   }
   options.add_experimental_option("prefs", prefs)

   return webdriver.Chrome(service=service, options=options)

URL = 'https://03plus.net/manage/login.php'

driver = initialize()

# ブラウザでurlを開く
driver.get(URL)

#IDの入力
account_id = driver.find_element(By.ID, 'login_id')
account_id.send_keys('xxxxx')
sleep(3)

#IDの入力
account_pw = driver.find_element(By.ID, 'login_passwd')
account_pw.send_keys('xxxxx')
sleep(3)

#ログイン
login = driver.find_element(By.ID, 'login_button')
login.click()
sleep(5)

#請求明細クリック
driver.get('https://03plus.net/manage/payment.php')
sleep(5)

#明細ダウンロードクリック
driver.find_element(By.LINK_TEXT, "ダウンロード").click()
sleep(10)

#ログアウトクリック
driver.find_element(By.LINK_TEXT, "ログアウト").click()
sleep(5)

#確認ボタン押下
from selenium.webdriver.common.alert import Alert
Alert(driver).accept()
sleep(5)

driver.quit()

最後まで手こずったのが、保存先を指定するところです。保存先を指定してSeleniumを初期化しているところですが、ブラウザをシークレットモードで起動するオプションを加えると設定が無効になるようです。調べてもこの情報が見当たらなかったので苦戦しました。

03fax.py
def initialize():
   new_driver = ChromeDriverManager().install()
   service  = ChromeService(executable_path=new_driver)
   
   options = webdriver.ChromeOptions()
   #ブラウザをシークレットモードで起動する
   options.add_argument('--incognito')
   #options.add_argument('--headless')
   prefs = {
       "download.default_directory": 'xxxxxxxx',
   }
   options.add_experimental_option("prefs", prefs)

   return webdriver.Chrome(service=service, options=options)

参考サイト

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