ruby中的win32ole使用

來源:互聯網
上載者:User

標籤: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使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.