VBScriptでのWEBサイト自動ログインプログラム作成方法

このQ&Aのポイント
  • VBScriptを使用して、WEBサイトにIDとPASSワードを自動入力してログインするプログラムを作成する方法を教えてください。
  • 以下のコードを使用してログインを試みましたが、13行目のエラー【オブジェクトがありません。】が発生しています。
  • ログインしたいページのソースコードも添付しています。
回答を見る
  • ベストアンサー

VBScript でのフォーム入力

VBScript でWEBサイトにID,PASSワードを入力し自動ログインするプログラムを作成しています。 以下のコードで実行していますが 13行目、1文字目【オブジェクトがありません。】とエラーが出ます。 どのようにすればログインできるか教えてください。 【VBScript】 ==================================================================== '---------- ログイン情報 ID = "myid" PASS = "mypass" '---------- ログイン画面表示 Set myIE = CreateObject("InternetExplorer.application") myIE.Visible = True myPAGE = "http://test1.com/" 'サンプルURLです。実際のURLは別です。 myIE.Navigate myPAGE Do Until myIE.busy = False And myIE.readyState = 4 : Loop '----------  ID/PASSの入力 myIE.Document.getElementById("login_id ").Value = ID myIE.Document.getElementById("password").Value = PASS myIE.Document.getElementById("checkbox").Checked = True Set objINPUT = myIE.Document.getElementsByTagName("INPUT") 'ループで頭からテキストが ログイン を探す For n = 0 To objINPUT.Length - 1 If objINPUT(n).Value = "ログイン" Then objINPUT(n).Click Exit For End If Next Set objINPUT = Nothing Set myIE = Nothing ==================================================================== 【ログインしたい http://test1.com/ ページの該当ソース】 ==================================================================== <form action="./login.php" method="post"> <div class="noti " width="900px"> <div> パスワードは5回連続で間違えると停止します。 </div> </div> <p> <label>ログインID</label> <input class="text-input" type="text" name="login_id" /> </p> <div class="clear"></div> <p> <label>パスワード</label> <input class="text-input" type="password" name="password" /> </p> <div class="clear"></div> <p id="remember-password"> <input type="checkbox" checked />パスワードを記憶 </p> <div class="clear"></div> <p> <input class="button" type="submit" value="ログイン" /> </p> </form> ==================================================================== よろしくお願いいたします。

質問者が選んだベストアンサー

  • ベストアンサー
  • notnot
  • ベストアンサー率47% (4846/10257)
回答No.1

getElementById("login_id ") となっていますが、HTMLの中で、id="login_id" がついているタグがありません。 以下同じ。 name属性で使いたいなら、getElementsByName を使ってください。 結果は配列なので、 myIE.Document.getElementsByName("login_id")[0].value = ID; と添え字をつけます。

kuwaosadahisa
質問者

お礼

回答ありがとうございました。 getElementsByName で解決できました。

その他の回答 (2)

  • unacyo
  • ベストアンサー率51% (35/68)
回答No.3

ANo.2です。追記。 私はIE9で確認していたのですが、IE8より前はidとname両方見ていたみたいですね。 IE8でidのみになったみたいですが、IE9で再びnameも見る様になっているみたいです…? 参考) http://wiki.clockahead.com/index.php?Coding%2FJavaScript%2FDOM%C1%E0%BA%EE%2FIE%A4%CE%CC%E4%C2%EA http://pgyougo.seesaa.net/article/113375690.html 一応、ANo.1さんの言うとおり、VerUPしたときのことも考えて、getElementsByName()に直しておいたほうが良いと思います。

kuwaosadahisa
質問者

お礼

回答ありがとうございました。 getElementsByName() に修正したら動きました。

  • unacyo
  • ベストアンサー率51% (35/68)
回答No.2

掲載したコードがそのままのコピー&ペーストであるならば、これが原因です。 →myIE.Document.getElementById("login_id△").Value = ID       △=半角スペース。login_idの後ろについている あと、ここが進むと、以下の行でエラーがでます。 → myIE.Document.getElementById("checkbox").Checked = True HTMLのcheckboxにnameが付いていません。 →<input type="checkbox" checked />パスワードを記憶 →<input type="checkbox" checked name="checkbox"/>パスワードを記憶    ※name="checkbox"を追加 これらを直すと、ログインページに飛べるみたいです。 ※私はgetElement~を使ったことがなかったのですが、動作を確認していてぐぐっていたら、getElementById()はIDで指定したものを見つけるとあるんですよね。 なんでnameで指定されたものも見つけるんでしょう?こういう仕様なんですかね? まぁ、今回は動いているからいいですよね、と思うことは思うのですが、ただ、ブラウザのVerがあがったり、違うブラウザになった場合、そのまま通用するかわかりませんので、このあたりは、仕様として認められているかどうか確認するか、頭の片隅にでも入れておいたほうが良いかもしれません。

kuwaosadahisa
質問者

お礼

回答ありがとうございました。 getElementById のため取得できなかったようです。 無事解決できました。

関連するQ&A

  • createElementが一瞬で消えてしまいます

    <script type="text/javascript"> <!-- function check(){ if(document.loginform.user.value==""){ var element1=document.createElement("span"); element1.innerHTML="Usernameを入力してください。"; element1.className="error0"; var ojbody1=document.getElementById("titlerror1"); ojbody1.appendChild(element1); false; } if(document.loginform.pass.value==""){ var element2=document.createElement("span"); element2.innerHTML="Passwordを入力してください。"; element2.className="error0"; var ojbody2=document.getElementById("titlerror2"); / ojbody2.appendChild(element2); false; } } // --> </script> <title>ログインページ</title> </head> <body> <div id="error"><font color="red"><?=$error?></font></div> <div id="loginform"> <form id="loginform" name="loginform" action="index.php" method="post"> <fieldset> <p> <div class="titlerror" id="titlerror1"> <label for="user">User Name <span class="hankaku">(半角英数字)</span></label><br /> </div> <input type="text" name="user" value="" id="user" text-align="middle"> </p> <p> <div class="titlerror" id="titlerror2"> <label for="pass">Password <span class="hankaku">(半角英数字)</span></label><br /> </div> <input type="password" name="pass" value="" id="pass" > </p> <div id="submit"> <p class="submit"> <button type="submit" name="login" value="login" class="button" id="login" onclick="check()"><img src="submit.png" border="0" alt="login" /></button> </p> </div> </fieldset> </form> </div> </body> javascript初心者です。 これを実行するとつくられたタグが一瞬だけ表示して消えてしまいます。(CSSを付けると) 上記は表示すらしません。 どこが間違っているか、なぜそうなのかを教えていただけると助かります。 お願いします。

  • wordpressのStyle設定について

    お世話になります。 仕事でwordpressを使うことになりテンプレートの作成を行なっています。 ヘッダーにログインフォームを付けたく「<?php wp_login_form( ); ?>」を使いログインフォームを表示させることはできました。 下記、ログインフォームのソースの抜粋になります。 <form name="loginform" id="loginform" action="***" method="post"> <p class="login-username"> <label for="user_login">ユーザー名</label> <input type="text" name="log" id="user_login" class="input" value="" size="20" tabindex="10" /> </p> <p class="login-password"> <label for="user_pass">パスワード</label> <input type="password" name="pwd" id="user_pass" class="input" value="" size="20" tabindex="20" /> </p> <p class="login-submit"> <input type="submit" name="wp-submit" id="wp-submit" class="button-primary" value="ログイン" tabindex="100" /> <input type="hidden" name="redirect_to" value="***" /> </p> </form></p> こんなかんじで表示させることはできたのですが、 この状態だと、自動的に<p>タグがはいってしまい改行されます。 この部分を横一行 ユーザ名[ ] パスワード[ ] [ログイン] のようにシたいのですがどのようにStyleをつけたらいいか悩んでます。 よろしければ、ご回答ください。

    • ベストアンサー
    • CSS
  • python スクレイピングでのログインができない

    スクレイピングをするために 下記のようなログインサイトにpythonでログインしたいです。 仮定url ="https://aaaaaaa/signin" <form> <p class="signin-label">ログイン画面</p> <ul class="signin-list"> <li class="signin-item"> <div class="hoge1"> <label class="hoge2">メールアドレス</label><div class="hoge3"> <input id="email" autocomplete="email" class="hoge4" type="email" required=""></div> </div> </li> <li class="signin-item"> <div class="hoge5"> <label class="hoge6">パスワード</label> <div class="hoge7"> <input id="password" autocomplete="new-password" class="hoge8" type="password" required=""></div> </div><p class="txt-right"> <a class="signin-link" href="/d/forgot_password">パスワードをお忘れの方</a></p></li></ul><div class="txt-center"> <button type="submit" class="hoge9">ログイン</button> </div> </form> ★行っているコード import requests from bs4 import BeautifulSoup from urllib.parse import urljoin # メールアドレスとパスワードの指定 MAIL = "aaa@mail.net" PASS = "555" # セッションを開始 session = requests.session() # ログイン login_info = { "email":MAIL, "password":PASS, } # action url_login = "https://aaaaaaa/signin" res = session.post(url_login, data=login_info) res.raise_for_status() # エラーならここで例外を発生させる res.text ※エラー内容 HTTPError: 404 Client Error: Not Found for url:https://aaaaaaa/signin 上記となります。 お知恵拝借いただけないでしょうか。

  • 同じ i 番目のclassを連動させることはできますか?

    お世話になります <script type="text/javascript"><!-- window.onload = function() { document.getElementsByClassName = function (className) { var i, j, eltClass; var objAll = document.getElementsByTagName ? document.getElementsByTagName("*") : document.all; var objCN = new Array(); for (i = 0; i < objAll.length; i++) { eltClass = objAll[i].className.split(/\s+/); for (j = 0; j < eltClass.length; j++) { if (eltClass[j] == className) { objCN.push(objAll[i]); break; } } } return objCN; } obj0 = document.getElementsByClassName("id"); obj1 = document.getElementsByClassName("pass"); for(i=0;i<obj0.length;i++) { obj0[i].onclick = function() { obj1[i].style.display = "block"; } } } //--></script> <style type="text/css"><!-- .id { width:150px; float:left; cursor:pointer; } .pass { display:none; } .pass0 { width:100px; } --></style> <div class="id">名前1</div> <div class="pass"> <input type="password" name="password" class="pass0"> <input type="button" name="login" value="LOG IN" class="login"> </div> <div class="id">名前1</div> <div class="pass"> <input type="password" name="password" class="pass"> <input type="button" name="login" value="LOG IN" class="login"> </div> のような感じでclass("id")[0]をクリックしたらclass("pass")[0]を表示、class("id")[1]をクリックしたらclass("pass")[1]を表示・・・というふうにしたいのですがなかなかうまくいきません>< 名前とパス両方を入力にしてやる方法もできますし、 それぞれのclass("id")にfunction()を振って obj0[0].onclick = function() { obj1[0].style.display = "block"; } obj0[1].onclick = function() { obj1[1].style.display = "block"; } のようなやり方もできるのはできます 後者のやり方は他の部分でもいろいろ使っているので記事やメンバーが増えるたびに新たに追加していかないといけないので***.onclick = function() { } を配列にするなどまとめてできる方法があれば教えていただきたいのですが^^;

  • パスワード入力フォームの実装で、IE6、7にも対応するには?

    入力フォームにデフォルト値でガイド的な文字を入れ フォームクリックでガイド文字が消え、入力できるようになるものを このサイトで教えてもらって実装したのですが Firefoxでは下記のソースで実装できたのですが IE6とIE7ではフォームに入力すると 入力した文字が●●●●ではなく、そのまま表示されてしまいます。 これを●●●●になるようできないでしょうか? 以下はサンプルで作ったソースです。 ------------------------------------------------------------------- <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=shift_jis"> <title>テスト</title> <script> <!-- function hide_pwd(pw){ if(pw == 'パスワード'){ document.getElementById("pwd").value =""; document.getElementById("pwd").setAttribute("type", "password"); } } // --> </script> </head> <body> <div class="id_box"><input type="text" name="id" onfocus="if (this.value == 'アカウントID') this.value='';" value="アカウントID"></div> <div class="pw_box"><input id="pwd" type="text" name="pass" onfocus="javascript:hide_pwd(this.value);" value="パスワード"></div> </body> </html> ------------------------------------------------------------------- たぶんjavascriptだと思うのですがよろしくお願いします。

  • style.displayだと効率悪いから...

    style.displayだと効率悪いからfor文を使いたいです。 以下はhtmlです。 なぜ効率悪いかというと、 一つ一つ、 セレクトごとに、 document.getElementById('Box1').style.display = "";と、 document.getElementById('Box1').style.display = "none"; を書かなければならないからです。 構文が長くなってしまい、非効率です。 しかし、document.getElementById自体1つしか書けないのです。 for文を使う必要があるのです。 ご教授願えたら幸いです。 <div class="col-auto my-1"> <label for="sample">選択してください</label> </div> <div class="col-auto my-1"> <select class="form-control" id="sample" onchange="viewChange();"> <option value="select1">one</option> <option value="select2">two</option> <option value="select3">three</option> </select> </div> <div class="col-auto my-5"> <div id="Box1" class="my-5"> <p>one</p> </div> <div id="Box2" class="my-5"> <p>two</p> </div> <div id="Box3" class="my-5"> <p>three</p> </div> </div> <script> function viewChange(){ if(document.getElementById('sample')){ id = document.getElementById('sample').value; if(id == 'select1'){ document.getElementById('Box1').style.display = ""; document.getElementById('Box2').style.display = "none"; document.getElementById('Box3').style.display = "none"; }else if(id == 'select2'){ document.getElementById('Box1').style.display = "none"; document.getElementById('Box2').style.display = ""; document.getElementById('Box3').style.display = "none"; } else if(id == 'select3'){ document.getElementById('Box1').style.display = "none"; document.getElementById('Box2').style.display = "none"; document.getElementById('Box3').style.display = ""; } } window.onload = viewChange; } </script>

  • style.displayだと効率悪いから…

    style.displayだと効率悪いから違うやり方をしたいです。 以下はhtmlです。 なぜ効率悪いかというと、 一つ一つ、 セレクトごとに、 document.getElementById(&#039;Box1&#039;).style.display = "";と、 document.getElementById(&#039;Box1&#039;).style.display = "none"; を書かなければならないからです。 構文が長くなってしまい、非効率です。 しかし、document.getElementById自体1つしか書けないのです。 for文を使う必要があるのです。 ご教授願えたら幸いです。 <div class="col-auto my-1"> <label for="sample">選択してください</label> </div> <div class="col-auto my-1"> <select class="form-control" id="sample" onchange="viewChange();"> <option value="select1">one</option> <option value="select2">two</option> <option value="select3">three</option> </select> </div> <div class="col-auto my-5"> <div id="Box1" class="my-5"> <p>one</p> </div> <div id="Box2" class="my-5"> <p>two</p> </div> <div id="Box3" class="my-5"> <p>three</p> </div> </div> <script> function viewChange(){ if(document.getElementById(&#039;sample&#039;)){ id = document.getElementById(&#039;sample&#039;).value; if(id == &#039;select1&#039;){ document.getElementById(&#039;Box1&#039;).style.display = ""; document.getElementById(&#039;Box2&#039;).style.display = "none"; document.getElementById(&#039;Box3&#039;).style.display = "none"; }else if(id == &#039;select2&#039;){ document.getElementById(&#039;Box1&#039;).style.display = "none"; document.getElementById(&#039;Box2&#039;).style.display = ""; document.getElementById(&#039;Box3&#039;).style.display = "none"; } else if(id == &#039;select3&#039;){ document.getElementById(&#039;Box1&#039;).style.display = "none"; document.getElementById(&#039;Box2&#039;).style.display = "none"; document.getElementById(&#039;Box3&#039;).style.display = ""; } } window.onload = viewChange; } </script> javascript全文を書いていただければ幸いです。

  • VBAでIE操作

    こんばんは。エクセル2003です。 証券会社にVBAでログインを試みてるのですができません。 ソースの一部を転記します。 <label for="account_id"><span>ユーザー名</span></label> <input type="text" tabindex="1" id="account_id" name="account_id" /> <label for="password"><span>パスワード</span></label> <input type="password" tabindex="2" id="password" name="password" /> <div id="rememberbox"> <label for="remember" id="rememberlabel">ユーザー名を記録する</label> <input type="checkbox" tabindex="3" name="remember" id="remember" /> </div> なので、下記VBAコードを作りました。 Sub IGマーケット証券() Set objIE = CreateObject("InternetExplorer.Application") objIE.Visible = True objIE.Navigate "http://www.igmarkets.co.jp/login/" Do While objIE.Busy = True DoEvents Loop Const READYSTATE_COMPLETE As Long = 4 Do Until objIE.ReadyState = READYSTATE_COMPLETE Loop objIE.Document.all("account_id").Value = "test" objIE.Document.all("password").Value = "test" objIE.Document.Forms(0).submit Set objIE = Nothing End Sub ですが、 objIE.Document.all("account_id").Value = "test" のところでエラーになってしまいます。 objIE.Document.all("login").Value = "test" に変えても同じです。 何がいけないのでしょうか? アドバイスよろしくお願いします。

  • ログインフォームの送信をURLでフォーム入力を省略

    メールや動画サイト等でログインする時、ID、パスワードを入力しますが、 面倒であるため省略しようと考えます。 例にニコニコ動画のログイン画面でのHTMLソースで<form><input>のみを抜粋します。(https://secure.nicovideo.jp/secure/ のソースです。) <form name="login" id="login" action="login" method="post"> <input type="hidden" name="next_url" value=""> <input value="" type="text" name="mail" id="mail"> <input value="" type="password" name="password" id="password"><input name="login" type="image" id="login_submit" src="img/login_form/btn_login.png" alt="ログイン"> </form> これをURLに置き換えると "https://secure.nicovideo.jp/secure/login?next_url=&mail=メールアドレス&password=パスワード" なると思うのですが"ログインエラー"になります。 わかる方よろしくお願いします。

  • パスワード入力フォームのガイドの制作するには・・・?

    入力フォームにデフォルト値でガイド的な文字を入れ フォームクリックでガイド文字が消え、入力できるようになるものを 作りたいのですが input type="text" のフォームはできましたが input type="password" のフォームに実装すると ガイド文字も●●●●と表示されてしまいます。 これを制作するのは無理なのでしょうか? 以下はサンプルで作ったソースです。 ------------------------------------------------------------------- <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=shift_jis"> <title>テスト</title> </head> <body> <div class="id_box"><input type="text" name="id" onfocus="if (this.value == 'アカウントID') this.value='';" value="アカウントID"></div> <div class="pw_box"><input type="password" name="pass" onfocus="if (this.value == 'パスワード') this.value='';" value="パスワード"></div> </body> </html> ------------------------------------------------------------------- たぶんjavascriptだと思うのですがよろしくお願いします。

専門家に質問してみよう