Win32ole in Ruby is a standard library that can be used as long as the require ' win32ole ' is added.
Here is a code to simulate a login
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
First line: Add Win32ole header.
Third line: Open an IE browser
Line five: IE is visible, if not write or false indicates a hidden window.
Line six: Use navigate to jump to a URL.
Line eighth: Wait for the page to load completely. If the page is busy, wait for 0.2s.
Line Nineth to tenth: Enter your account password. Here is the knowledge about the HTML DOM tree.
Ten-line and 12-line: Analog Click the login button. There are three ways to access HTML elements, Getelementbyid,getelementsbytagname,getelementsbyclassname (not available in ie5,6,7,8). If the element has an ID, of course, the ID is better, can be accurately positioned. But there is no ID this attribute, that can only use the other. I used getElementsByTagName, and this method will return all the names of the elements that are parameters (this example is input). However, this collection cannot be accessed using [], it will say that there is no such error or method, it needs to be called with each. See the official documentation for each method: Iterates over each item of the OLE collection which has ienumvariant interface, stating that the iteration element is an interface that has a calling property. In addition, some editors with Chinese will have errors, but also those coding problems, such as line 11th, then use another attribute is good.
Line 13th: Exit.
Win32ole in Ruby is used