PythonとSeleniumとWebDriverでDownload完了簡易判定と待機

Plan01~02:pythonistaの黒魔術的なコード
Plan03~04:pythonistaも読みにくいコード
Plan05~10:pythonistaが読みやすいコード
Plan11~12:pythonistaらしくはないコード

C:\Users\USERNAME\Downloadsがダウンロードフォルダの場合のコードです。
C:\Users\USERNAME\Downloadsにfoo.crdownloadやbar.tmpがあるとループ処理状態になります。

sample.js

import datetime
import itertools
import os
import time


def main() -> None:

    print("PRESS ENTER KEY TO BOOT")
    input()

    basyo: str = ""
    jikan: str = ""
    dtnow: datetime.datetime = datetime.datetime.now()

    # ----------------------------------------------------------
    # Plan01
    # ----------------------------------------------------------
    print("Plan01")
    _ = all([x for x in os.listdir(f"{os.getenv('USERPROFILE')}\\Downloads") if x.endswith(("crdownload", "tmp"))] and not time.sleep(1) and not print(f"{f'{datetime.datetime.now()}'[:-4]} download in progress...") and i for i in itertools.repeat(1))

    # ----------------------------------------------------------
    # Plan02
    # ----------------------------------------------------------
    print("Plan02")
    while [x for x in os.listdir(f"{os.getenv('USERPROFILE')}\\Downloads") if x.endswith(("crdownload", "tmp"))] and not time.sleep(1) and not print(f"{f'{datetime.datetime.now()}'[:-4]} download in progress..."):
        pass

    # ----------------------------------------------------------
    # Plan03
    # ----------------------------------------------------------
    print("Plan03")
    while [x for x in os.listdir(f"{os.getenv('USERPROFILE')}\\Downloads") if x.endswith(("crdownload", "tmp"))]:
        _ = time.sleep(1) or print(f"{f'{datetime.datetime.now()}'[:-4]} download in progress...")

    # ----------------------------------------------------------
    # Plan04
    # ----------------------------------------------------------
    print("Plan04")
    while [x for x in os.listdir(f"{os.getenv('USERPROFILE')}\\Downloads") if x.endswith(("crdownload", "tmp"))]:
        time.sleep(1)
        print(f"{f'{datetime.datetime.now()}'[:-4]} download in progress...")

    # ----------------------------------------------------------
    # Plan05
    # ----------------------------------------------------------
    print("Plan05")
    basyo = f"{os.getenv('USERPROFILE')}\\Downloads"
    while [x for x in os.listdir(basyo) if x.endswith(("crdownload", "tmp"))]:
        time.sleep(1)
        jikan = f"{datetime.datetime.now()}"[:-4]
        print(f"{jikan} download in progress...")

    # ----------------------------------------------------------
    # Plan06
    # ----------------------------------------------------------
    print("Plan06")
    basyo = f"{os.getenv('USERPROFILE')}\\Downloads"
    while [x for x in os.listdir(basyo) if x.endswith(("crdownload", "tmp"))]:
        time.sleep(1)
        dtnow = datetime.datetime.now()
        jikan = dtnow.strftime("%Y-%m-%d %H:%M:%S.%f")[:-4]
        print(f"{jikan} download in progress...")

    # ----------------------------------------------------------
    # Plan07
    # ----------------------------------------------------------
    print("Plan07")
    basyo = str(os.getenv("USERPROFILE")) + "\\Downloads"
    while [x for x in os.listdir(basyo) if x.endswith(("crdownload", "tmp"))]:
        time.sleep(1)
        dtnow = datetime.datetime.now()
        jikan = dtnow.strftime("%Y-%m-%d %H:%M:%S.%f")[:-4]
        print(jikan + " download in progress...")

    # ----------------------------------------------------------
    # Plan08
    # ----------------------------------------------------------
    print("Plan08")
    basyo = str(os.getenv("USERPROFILE")) + "\\Downloads"
    while [x for x in os.listdir(basyo) if x.endswith(("crdownload", "tmp"))]:
        time.sleep(1)
        dtnow = datetime.datetime.now()
        jikan = dtnow.strftime("%Y-%m-%d %H:%M:%S.%f")
        print(jikan + " download in progress...")

    # ----------------------------------------------------------
    # Plan09
    # ----------------------------------------------------------
    print("Plan09")
    basyo = str(os.getenv("USERPROFILE")) + "\\Downloads"
    while [x for x in os.listdir(basyo) if x.endswith(("crdownload", "tmp"))]:
        time.sleep(1)
        print("download in progress...")

    # ----------------------------------------------------------
    # Plan10
    # ----------------------------------------------------------
    print("Plan10")
    basyo = str(os.getenv("USERPROFILE")) + "\\Downloads"
    while [x for x in os.listdir(basyo) if x.endswith(("crdownload", "tmp"))]:
        time.sleep(1)

    # ----------------------------------------------------------
    # Plan11
    # ----------------------------------------------------------
    print("Plan11")
    matsu = True
    basyo = str(os.getenv("USERPROFILE")) + "\\Downloads"
    while matsu:
        matsu = False
        for x in os.listdir(basyo):
            if x.endswith(("crdownload", "tmp")):
                matsu = True
        if not matsu:
            break
        time.sleep(1)

    # ----------------------------------------------------------
    # Plan12
    # ----------------------------------------------------------
    print("Plan12")
    matsu = True
    basyo = str(os.getenv("USERPROFILE")) + "\\Downloads"
    while matsu:
        matsu = False
        for x in os.listdir(basyo):
            if x.endswith("crdownload") or x.endswith("tmp"):
                matsu = True
        if not matsu:
            break
        time.sleep(1)

    print()
    print("PRESS ENTER KEY TO EXIT")
    input()


if __name__ == "__main__":
    main()

あとがき

ノンプログラマーの素人が記述をしたコードです。
狭い利用範囲と少ない利用頻度での確認ですので、
記載内容に間違いや勘違いがあるかもしれません。
上記内容を参照の際は自己責任でお願い致します。

pythonista pythonist pythonian pythoner