標籤:style blog color io 使用 ar sp div 問題
ruby中的win32ole是一個標準庫,使用的時候只要添加require ‘win32ole‘就行。
下面是一段類比一個登陸的代碼
1 require ‘win32ole‘ 2 3 ie = WIN32OLE.new(‘internetExplorer.application‘) 4 5 ie.visible = true 6 ie.navigate(‘www.***.cn‘) 7 8 sleep(0.2) until ie.busy == false 9 ie.Document.getElementById(‘loginId‘).value = "*****"10 ie.Document.getElementById(‘password‘).value = "*****"11 #ie.Document.getElementsByTagName(‘input‘).each {|x| x.click if x.value == "登陸"}12 ie.Document.getElementsByTagName(‘input‘).each {|x| x.click if x.name == "user_login"}13 ie.quit
第一行:添加win32ole頭。
第三行:開啟一個ie瀏覽器
第五行:ie可見,如果不寫或者false則表示隱藏視窗。
第六行:使用navigate跳轉到一個網址。
第八行:等待頁面載入完全。如果頁面忙,則等待0.2s。
第九行到第十行:輸入帳號密碼。這裡用到的是HTML DOM樹的相關知識。
十一行和十二行:類比點擊登入按鈕。有三種方法可以訪問html元素,getElementById,getElementsByTagName,getElementsByClassName(在IE5,6,7,8中不能用)。如果元素有id的話當然用id比較好,可以準確的定位。但是沒有id這個屬性的話,那隻能用其他的了。我用了getElementsByTagName,這個方法會返回所有的名字是參數(本例是input)的元素的集合。但是這個集合不能用[]來訪問,它會說沒有這個錯誤或者方法,需要用each來調用。參看each方法官方文檔:Iterates over each item of OLE collection which has IEnumVARIANT interface,說明迭代元素是有調用屬性的介面的。另外,有些編輯器用中文會出現錯誤,又是那些編碼的問題,如第十一行,那麼用另一個屬性就好了。
第十三行:退出。
ruby中的win32ole使用