LoginSignup
2
1

More than 3 years have passed since last update.

IEで自動ログインする際の注意点(新しい窓を追いかける)

Posted at

最初のログインページが空窓になり捨てられる

 IEを動かせると知って、最初にやりたいと思うのが社内システムの自動ログインではないでしょうか。個人ではあまりIE使わないでしょうし。
 ただ、これがなかなか難しい。うまくいかない最大の要因は、ログインのURLを踏むと、その窓は空窓になり、新しい窓にログイン画面が表示されるというページ移動にあります。
 IEを動かしてURLに飛んでも、その窓が空窓として捨てられてしまい、動かせなくなるというわけです。
 なので、窓を追いかけることについて書こうと思います。

追いかける方法

  • shell.applicationで開かれているウインドウの中からログインページをIEオブジェクトとして捕捉し直す
  • WScript.ShellのAppActivateでログインページをアクティブにし,SendKeysによってID,パスワードを入力する

 SendKeysメソッドは誤作動が多いですし,最後の手段的なものなので,おすすめは前者です。

スクリプト

VBScript
    'ID,PWを設定
    Const id = "user"
    Const password = "pass"

    Dim ie
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Navigate ("ログイン画面を開くURL")
    Wscript.Sleep 3000
    Dim win
    Dim shellApp
    Set shellApp = CreateObject("Shell.Application")
        For Each win In shellApp.Windows
            If win.Name = "Internet Explorer" And win.LocationURL = "ログイン画面のURL" Then
                Set ie = win
                Exit for
            End If
        Next
    Dim htmldoc
    Set htmldoc = ie.document
    htmldoc.getElementsByTagName("INPUT")(1).Value = id
    htmldoc.getElementsByTagName("INPUT")(2).Value = password
    'だいたい3か4辺りにあるログインボタンを押す
    htmldoc.getElementsByTagName("INPUT")(4).Click
新しい窓を補足する
    Dim win
    Dim shellApp
    Set shellApp = CreateObject("Shell.Application")
        For Each win In shellApp.Windows
            If win.Name = "Internet Explorer" And win.LocationURL = "ログイン画面のURL" Then
                Set ie = win
                exit for
            End If
        Next

 新しい窓は,LocationURLもしくはLocationNameで見つけます。いずれにせよ,捨てられた窓を間違って拾わない方法で補足する必要があります。
 補足したら,後はHTMLを確認してID,パスワード,ログインボタン名を特定して入力,クリックするだけです。

 このあたりは,PowerShellを併用して確認すると早いですよ。

2
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
2
1