from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from pathlib import Path
import time
import json
# 保存対象URL一覧取得
urls = []
with open('urls.txt', mode='rt', encoding='utf-8') as f:
urls = f.readlines()
# Chrome の印刷機能でPDFとして保存
options = webdriver.ChromeOptions()
# PDF印刷設定
appState = {
"recentDestinations": [
{
"id": "Save as PDF",
"origin": "local",
"account": ""
}
],
"selectedDestinationId": "Save as PDF",
"version": 2,
"pageSize": 'A4'
}
# ドライバへのPDF印刷設定の適用
options.add_experimental_option("prefs", {
"printing.print_preview_sticky_settings.appState":
json.dumps(appState),
"download.default_directory": '~/Downloads'
})
options.add_argument('--kiosk-printing')
with webdriver.Chrome("./chromedriver.exe", options=options) as driver:
# 任意のHTMLの要素が特定の状態になるまで待つ
wait = WebDriverWait(driver, 15)
for url in urls:
driver.implicitly_wait(10)
driver.get(url)
# ページ上のすべての要素が読み込まれるまで待機
wait.until(EC.presence_of_all_elements_located)
# PDFとして印刷
driver.execute_script('window.print()')
# 待機
time.sleep(10)
driver.quit()