[ruby-list:49966] Re: [質問] RubyからIE操作 ie.Document.All.???.click

From: <yamataka@...08.itscom.net>
Date: 2014-09-11 05:00:06 UTC
List: ruby-list #49966
山口です。 

> 助田です。

ずっと、ご教示、ありがとうございます。

> On Wed, Sep 10, 2014 at 01:31:14PM +0900, yamataka@u08.itscom.net 
wrote:

[...]

> だとすると、私が、想像で思いつくのは
> print ie.document.url + "\n"
> の実行のタイミングが早すぎるんじゃないかということ
> ぐらいです。
> 
> 勤務画面の表示が終わるより前に
> ie.document.urlが実行しているために
> 遷移する前の最初のURLが表示されてしまうのではないかと
> 思います。
> 
> sleepで何秒か処理を遅らせてから
> 
>   print ie.document.url + "\n"
> 
> すると
> https://kinmu.case.foo.co.jp/cndb03/WPSM_Menu.P_MainFrame
> になりませんか?

試しに、sleep(10) を入れてみたら、
助田さんのご推測どおり、
url  :https://kinmu.case.foo.co.jp/cndb03/WPSM_Menu.P_MainFrame
なりました。
 
> また、勤務画面に遷移した後で実行した
> puts ie.document.all(0).outerHTML
> (かまたは、puts ie.document.all(1).outerHTML)
> の出力結果と
> ブラウザのメニューからソースを表示したときの内容は
> 同じになりますか?

ie.document.all(0).outerHTML と ie.document.all(1).outerHTML 出力結果の
差異は、
all(0) 側には、
</HEAD><FRAMESET onunload="return fnUnLoad()" cols="40%,60%     "><FRAME 
src="/cndb03/WPSM_Menu.P_Menu?szShimeinoCookie=00009107001864333973&amp;
szShimeDateCookie=&amp;szKyotenCdCookie=10008794043600000000&amp;
nSessionNoCookie=2026&amp;szKaishaCDCookie=500100&amp;szKanriShimeiNo=&
amp;szKanriKyotenCd=" name=MENU><FRAME src="/cndb03/WPSM_Menu.P_Version" 
name=MAIN></FRAMESET></HTML>
があり、
all(1) 側には、ありません。
他は同じでした。

> 同じになれば、そこから frameを経由して勤務一覧リンクに
> たどりつける可能性があると思います。

ブラウザは、勤務画面を表示しており、左フレームの勤務一覧のリンクが、ie.
document.all(0).outerHTMLからは見えない状況です。

# 勤務画面のフレームコレクションを取得
frame_collection = ie.document.frames
# ie.document.all(0).outerHTML に、FRAME srce="..." があるので、frame_
colection.item(0) を target_frame にする 
target_frame = frame_collection.item(0)  # 下のtarget_frame.documentで、
error:  method_missing: document (WIN32OLERunTimError)
# 勤務画面 左側のフレームのドキュメントを取得
document = target_frame.document
で、エラーになってしまいます。

以下、ソースと出力結果を付けますので、大変恐縮ですが、引き続きご教示いた
だければ幸いです。
■ ソース
$ cat x.rb
#!/bin/ruby
# -*- coding: utf-8 -*-

# refer following web pages
# http://www.tech-notes.dyndns.org/win32ole/ie_ctrl.html
# http://atomic.jpn.ph/prog/etc/ieform.html
# http://www.geocities.co.jp/SiliconValley-Bay/3475/ie.html
# print "location.href:" 

DEBUG=true

URL="http://www.case.foo.co.jp/"

# 共用認証画面 HTMLソース内のTAGのname
#<td><input type="text" name="UID" id="UID" accesskey="U" autocomplete="
off" maxLength="30" tabindex="1" onKeyPress="keyPressCheck(event,1);"></
td>
#<td><input type="password" name="PASS" id="PASS" accesskey="P" 
autocomplete="off" maxLength="29" tabindex="2" onKeyPress="keyPressCheck
(event,1);"></td>
ITEM_UID   = "UID"
ITEM_PASS  = "PASS"
ITEM_LOGIN = "IDLOGIN"

# 自分のIDとパスワード
UID  = "XXX"
PASS = "YYY"

require 'win32ole'

ie = WIN32OLE.new("InternetExplorer.Application")
ie.visible = true

# 指定URLを表示
ie.navigate URL
sleep(1) until ie.ReadyState == 4

# "勤務 login" ボタンを押す
ie.document.all.b_kinmu_login.click
sleep(1) until ie.ReadyState == 4

# ブラウザは画面遷移しているが、ie.document等の実行がをすぐに
# 行なうと失敗するので、「仮で」10秒スリープさせる
sleep(10)

print "url  :" + ie.document.url   + "\n" if DEBUG
print "title:" + ie.document.title + "\n" if DEBUG

if ie.document.url =~ /.*Login\?AccessID.*/ then # 共用認証画面であれば

  # 共用認証画面のIDとPASSWORDを入力
  ie.document.all.Item(ITEM_UID).Value  = UID
  ie.document.all.Item(ITEM_PASS).Value = PASS

  # 共用認証画面のログインボタンを押し、勤務一覧画面に遷移させる
  ie.document.all.Item(ITEM_LOGIN).click() 
end

# 勤務画面のHTMLソースを出力
if DEBUG then
print "***** all(0) ***** \n"
print ie.document.all(0).outerHTML

print "\n\n\n"
print "***** all(1) ***** \n"
print ie.document.all(1).outerHTML
end

# 勤務画面のフレームコレクションを取得
frame_collection = ie.document.frames

# ie.document.all(0).outerHTML に、FRAME srce="..." があるので、frame_
colection.item(0) を target_frame にする 
target_frame = frame_collection.item(0)  # 下のtarget_frame.documentで、
error:  method_missing: document (WIN32OLERunTimError)

# 勤務画面 左側のフレームのドキュメントを取得
document = target_frame.document

# ドキュメントの中のリンクのコレクションを取得
link_collection = document.body.all.tags("A")

# リンクのコレクションの中から「勤務一覧」に一致する
# リンクを探して見つかったらクリック
link_collection.each {|link|
  if link.innerText == "勤務一覧"
    link.click
  end
}

■実行結果
$ ./x.rb > /tmp/tako 2>&1
$ cat /tmp/tako
url  :https://kinmu.case.foo.co.jp/cndb03/WPSM_Menu.P_MainFrame
title:Case/勤務
***** all(0) ***** 
<HTML><HEAD><TITLE>Case/勤務</TITLE>
<META name="" content="text/html; charset = Shift_JIS" http-equiv=
Content-type>
<SCRIPT language=JavaScript>
<!--
 var calcWindow=null;
 var isWindowExec=0;
 var progressWindow=null;
 var isProgressExec=0;
 var errorWindow=null;
 var isErrorExec=0;
function fnUnLoad(){
 if(isWindowExec==0){
 if(calcWindow!=null){
 if(calcWindow.closed==false){
 calcWindow.close();
 }
 }
 }
 if(isProgressExec==0){
 if(progressWindow!=null){
 if(progressWindow.closed==false){
 progressWindow.close();
 }
 }
 }
 if(isErrorExec==0){
 if(errorWindow!=null){
 if(errorWindow.closed==false){
 errorWindow.close();
 }
 }
 }
 return true;
}
// Used Jave Script-->
</SCRIPT>
</HEAD><FRAMESET onunload="return fnUnLoad()" cols="40%,60%     "><FRAME 
src="/cndb03/WPSM_Menu.P_Menu?szShimeinoCookie=00009107001864333973&amp;
szShimeDateCookie=&amp;szKyotenCdCookie=10008794043600000000&amp;
nSessionNoCookie=2026&amp;szKaishaCDCookie=500100&amp;szKanriShimeiNo=&
amp;szKanriKyotenCd=" name=MENU><FRAME src="/cndb03/WPSM_Menu.P_Version" 
name=MAIN></FRAMESET></HTML>


***** all(1) ***** 
<HEAD><TITLE>Case/勤務</TITLE>
<META name="" content="text/html; charset = Shift_JIS" http-equiv=
Content-type>
<SCRIPT language=JavaScript>
<!--
 var calcWindow=null;
 var isWindowExec=0;
 var progressWindow=null;
 var isProgressExec=0;
 var errorWindow=null;
 var isErrorExec=0;
function fnUnLoad(){
 if(isWindowExec==0){
 if(calcWindow!=null){
 if(calcWindow.closed==false){
 calcWindow.close();
 }
 }
 }
 if(isProgressExec==0){
 if(progressWindow!=null){
 if(progressWindow.closed==false){
 progressWindow.close();
 }
 }
 }
 if(isErrorExec==0){
 if(errorWindow!=null){
 if(errorWindow.closed==false){
 errorWindow.close();
 }
 }
 }
 return true;
}
// Used Jave Script-->
</SCRIPT>
</HEAD>

./x.rb:72:in `method_missing': document (WIN32OLERuntimeError)
    OLE error code:80070005 in <Unknown>
      アクセスが拒否されました。

    HRESULT error code:0x80020009
      例外が発生しました。
	from ./x.rb:72






In This Thread