Win32ole in Ruby is used

Source: Internet
Author: User



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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.