0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Selenium Basic VBA で、ログイン状態を保ったままChromeを起動する

Last updated at Posted at 2021-06-09

Seleniumでスクレイピングするとき、
スクレイピング用のChromeが立ち上がるとログイン状態が消えてしまう。
で、面倒なタイプのログインを要求するサイト(人間ですか?とか、メール送れとか)
だと、スクレイピングできない。

そこで、一度手動でログインしていれば
スクレイピング用のChromeもそのログイン状態を保ったまま起動できるという方法がある。
で、それのVBA版のいいやつがなかったので書いた。
参照設定はなんか必要な気がする。
ChromeDriverの取得などは別途やってください。

    Set Driver = New ChromeDriver '出力用のChromeDriver
    'ユーザーデータ用フォルダを作る
    Set wsh = CreateObject("WScript.Shell")
    path = wsh.SpecialFolders("MyDocuments") & "\chrome_logined"
    If Dir(path, vbDirectory) = "" Then MkDir (path)

    'ユーザーディレクトリを指定しつつChrome起動
    Driver.AddArgument ("user-data-dir=" & path)
    Driver.Start "chrome"
    
    '目的のサイトにアクセス
    Driver.Get "https://**********************"
    '※ここに、サイトのロードを待機するコードを書く

    '次に、ログインできていない時の判定文を書く。「Pleas login」と書かれたエレメントがあったらログインしてないとか。
    If ログイン失敗
        SetForegroundWindow Application.hwnd
        MsgBox "ログイン履歴を作るため、このツールで開いたブラウザで、一回ログインしてください。" & _
                vbCr & vbCr & "ログインできたら、このツールを再度実行してください", vbInformation
    Else
       'ログインしたあとの処理
    end if
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?