Go to pamie-python to Implement IE automation module (with NetEase registration code)

Source: Internet
Author: User
Pamie-Python implements ie automation module (with NetEase registration code)

Pamie is a set of tools written for python for automated Web testing. It is implemented using win32com to operate IE.
Pamie is a good ie Operation Module
It is convenient to use pamie to operate ie browsers. Originally, it was an item for IE testing automation,
In this way, it is easy to use pamie to capture webpages and submit forms automatically.

Usage:
To test a simple example:

From cpamie import pamie

Ie = pamie ()

Ie. navigate ("Google.com ")
Ie. textboxset ('Q', 'python ')
Ie. buttonclick ('btng ')
Ie. linkclick ('python Programming Language -- Official website ')
Ie. windowchange ('python Programming Language -- Official website ')
Ie. windowfind ('python Programming Language -- Official website ')
Ie. textboxset ('Q', "Automation ")
Ie. buttonclick ('submit ')

Directly operate the form element on the page and access the text information of the page to determine whether the user login is successful.

In pamie, how does one deal with the pop-up dialog box of Chinese titles?
Http://www.python-forum.org/pythonforum/viewtopic.php? F = 17 & t = 3194
Http://markmail.org/message/u3ojkyj2ns7p3dn3#query:+page:1+mid:5wuwfctervph6tlv+state:results
Python:

Import cpamie, cmodalpopup, winguiauto
Import time

Ie = cpamie. pamie ()
WGA = winguiauto

# Logon
Ie. navigate ('d: \ Python \ webdialogtest \ 1.html ')

# Start a new process and automatically click "cancel"
Clickcancel = cmodalpopup. handlepopup ("Confirm", "cancel ")
Clickcancel. popupname = "select file"
Clickcancel. Start ()

Ie. buttonclick ("Install ")
Time. Sleep (1)

Clickcancel. Join () # process ended

Certificate -------------------------------------------------------------------------------------------------------------------------------------

Another introduction:

Pamie is an interesting module. It is a module for implementing IE automation. Previously, I used the clientcookie module, which can use urllib2 to access the page through a program and is suitable for environments that require cookie support. However, some web access processes are not just like downloading files, but sometimes a system action. Pamie should be a more intuitive and simple process of simulating manual input. However, the limitation is that it only supports IE, which can be used in some special cases.

To use it, you need to install the win32all module.

The following is a simple example provided by readme:

Import cpamie

Ie = cpamie. pamie ()

# Start script:

Ie. navigate ('HTTP: // pamie.sourceforge.net/pamieform.html ')
Ie. settextbox ('john', 'firstname', 0)
Ie. settextbox ('doe ', 'lastname', 0)
Ie. settextbox ('2014 State Street ', 'addline1', 0)
Ie. settextbox ('suite # 16', 'addline2', 0)
Ie. settextbox ('san Mateo ', 'city', 0)
Ie. setlistbox ('CA', 'state', 0)
Ie. settextbox ('20140901', 'zip', 0)

Ie. clickbutton ('submit ', 0)

After we download this module, it is a zip package. It is not in the installation mode. You can copy cpamie. py to the Lib subdirectory under the python installation directory. The above code is very simple. Pilot the cpamie module and generate an Automation Object IE. At this time, we will see that the program automatically opens an IE window, and our operations can be seen in this ie window, the executed part is displayed in yellow.

Ie. navigate () is used to access a link.
Ie. settextbox () is used to set the value of a text field. The first parameter is the input value, and the second parameter is the name of the input field of the corresponding form table (you need to check the source code ), the third is the form name (in this example, 0 indicates the subscript ).
Ie. setlistbox () is used to set the value of the list box.
Ie. clickbutton () is used to simulate clicking a button. The first parameter is the button name, and the second parameter is 0 to indicate the subscript.

Some other available methods, such as clicklink (), are written on the pamie homepage (). Its first parameter is the text corresponding to the link. If it is Chinese, convert it to Unicode. For example, if the page is gb2312, the source code of the link is:

<A href = "http://xxx.com"> Chinese characters </a>

The call is as follows:

Ie. clickbutton (Unicode ('kanji ', 'cp936 '))

This is not required in actual operation, as long as the parameter is Unicode encoding.

If you view the source code, you can also see some other methods:

Quit () -- disable open IE
Goback () -- back
Refresh () -- refresh

If you are interested, you can give it a try. You can see the results on one side of the operation, which is very simple and convenient.

Bytes -------------------------------------------------------------------------------------------------------------------------------

Article 3

Python is simple and clear, and modules or component modules provided by third parties are also simple and clear.

The following is an example listing more comprehensive code and annotations...

#-*-Coding: cp936 -*-
Import sys

# I put all the files in pamie2.0 such as cpamie in the folder named pamie in the following path and added sys. Path to facilitate the import of all the modules in the pamie folder.
# As long as cpamie can be imported, the example here can be run.
SYS. Path. append (R "F: \ python25 \ Lib \ Site-packages \ pamie ")
Import cpamie

# New instance
Ie = cpamie. pamie ()

# Open the target webpage

Ie. navigate ('HTTP: // pamie.sourceforge.net/pamieform.html ')

# Note that the first parameter listed below, such as fistname, is the input name = "firstname" in the source code of the webpage"
# You cannot use the italics displayed on the webpage: "First name :"
# Text box
Ie. textboxset ('firstname', 'justthisname ')

Ie. textboxset ('lastname', 'iamhere ')

# Select the drop-down menu:

Ie. listboxselect ('state', "Ms ")

# Square selection box:

Ie. checkboxset ('cxbx ', 1) #1 2 3 or * are all "checked" meaning ''Null String indicates unselected

# Circle selection box: the names of these elements are easy to know when radio has been designed for Web pages.
# Select jazz here
Ie. radiobuttonset ("Music", "Jazz") # Music is the name of the selection list. Find it from the source code of the webpage.

# Final submit button

Print "now submit button, as long as you input the following sentence in the python interpreter, submit"
# It is much easier for IE. buttonclick ("Submit") to replace mouse clicks

# Input type = "Submit" name = "Submit" in the source code. Therefore, the first parameter is the value of name "Submit"
# Pamie is case-sensitive.

# Ie. buttonclick ("Submit ")

# IE has its own features. It seems that the use of pamie to open multiple webpages is added to the same ie process, not a webpage or a process.

''' Many web pages are complex, such as image buttons and JavaScript buttons.
Use ie. buttonimageclick ("submitregister ")

Some buttons will execute JavaScript to submit the content after clicking the mouse, for example, the content in the source code is:
<Input type = "image" src = "http://www.sit.com/member/images/register.gif" width = "100" Height = "54" border = "0"
Onclick = "javascript: submitme ();
That is to say, when you click the image called register.gif, javascript: submitme () will be executed to submit the page:

Ie. javascriptexecute ("javascript: submitme ()") to automatically submit pages. After you move these buttons to the lower left corner of IE, the corresponding

Javascript: thefunction () is easy to find...

It is also possible to execute a JavaScript function with parameters... ie. javascriptexecute ("javascript: submitme ('param ')")
You can also directly set the value of an element in a webpage:
For example, if document. Form. Id. value = "string" is set in the webpage source file, the following statements can be directly modified:

Ie. javascriptexecute ("javascript: Document. Form. Id. value = 'thevalueyouwant '")

In addition, pamie2.0 has a module for processing simple pop-up windows. The title of such windows is generally Microsoft Internet Explorer.

Import cmodalpopup

A = cmodalpopup. handlepopup ('Confirm', "OK") # "the second parameter is" OK ", indicating that the text on the button in the pop-up window is" OK"

:

Run:

A. Run () can be used to click the "OK" button in the "pop-up window...
Multi-threaded programs can be used to process such windows with one thread. This will not affect the main program...

Another useful table filling tool for pamie: airoboform.exe

After installing the IE Toolbar tool, you can click "save" for example to save the webpage table, move the mouse to "Baidu" for example, stop for a moment, and a submenu is displayed. Select "edit" to view all the names of the form on that page. there are so many "first Parameters" that you do not need to find the text box or the name of the list box, you can see the "name" of all the boxes to be filled in the opened Editor. All the information displayed is "lower case, check whether the first letter is in uppercase in the source file... pamie is case-sensitive. airoboform is case-insensitive...

Pamie2.0 also has the following limitations:

Below is a reference from http://yinxl.spaces.live.com/blog/cns! 4626e4f8c0bfc0bc! 172. Entry "extension patch ".

In May 14, pamie was able to easily control a new ie window and write a web test program. According to ZV's recommendation, it was implemented using python, this is because the open-source package of pamie is very useful for testing web pages. However, when pamie is used, it is found that its findwindow function is very difficult to use, which makes it difficult for the test program to control the new ie window, such as HTML: <a href = "B .html" target = "_ blank"> link_content </a>: The New ie window opened by this link. You can't find a proper solution by reading the pamie source code. I have modified cpamie of pamie by referring to the IEC open source package method. the constructor of The py pamie class. The modified constructor can find the IE window that has been found based on the entered URL address. If no matching window is found, pamie creates a window to open the URL. The modified code is as follows: From win32com. Client import dispatch # The Import Statement to be added #2006-5-10 modified by yinxianglong to find a opened ie window. Begin
Def _ init _ (self, url = none, isfind = false, timeout = 300 ):
"The class instantiation code. When the object is instantiated you can
Pass a starting URL. If no URL is passed then about: blank, a blank
Page, is brought up.
Parameters:
[Url]-URL to navigate to initially
[Isfind]-If isfind = true, find a opened ie window whose locationurl is equal to URL
[Timeout]-How many 100 ms increments to wait, 10 = 1sec, 100 = 10sec
Returns:
Nothing
"""

Self. showdebugging = true # Show debug print lines?
Self. colorhighlight = "# f6f7ad" # Set to none to turn off highlighting
Self. framename = none # The current frame name or index. nested frames are
# Supported in the format frame1.frame2. frame3
Self. formname = none # The current form name or index
Self. busytuner = 1 # number of consecutive checks to verify document is no longer busy. find_ OK _flag = false
If isfind and (URL! = ''):
CLSID = '{9ba05972-f6a8-11cf-a442-00a0c90a8f39}' # CLSID for shellwindows
Shellwindows = Dispatch (CLSID)
Url = URL. Lower ()
For I in range (shellwindows. Count ):
If shellwindows [I]. locationurl. Lower (). Find (URL)>-1:
Self. _ Ie = shellwindows [I]
Find_ OK _flag = true
If not find_ OK _flag:
Print "can't find the opened ie window whose locationurl is equal to URL ."
Self. _ Ie = dispatchex ('internetexplorer. application ')
If URL:
Self. _ ie. navigate (URL)
Else:
Self. _ ie. navigate ('about: blank ')

Self. _ timeout = timeout
Self. _ ie. Visible = 1
Self. Timer = datetime. datetime. Now ()
#2006-5-10 modified by yinxianglong to find a opened ie window. EndOther codes are the same as those of pamie2.0.The following is the test code: Import yxlpamie Ie = yxlpamie. pamie ("http: // 172.17.153.171/newsys/default. aspx") print ie. pagegettext ()
The test result shows the HTML code of the opened http: // 172.17.153.171/newsys/default. aspx window.

Appendix: Netease registration code

# Coding: GBK
Import cpamie
Import time
Import random
Username = ''. Join (random. Sample ('abcdefghijklmnopqrstuvwxyz0123456789 ', 8 ))
Password = '******'
Zhanghao = '|'. Join ([username, password])
Ie = cpamie. pamie ()

# Open the target webpage

Ie. navigate ('HTTP: // reg.163.com/reg0.shtml ')

# Note that the first parameter listed below, such as fistname, is the input name = "firstname" in the source code of the webpage"
# You cannot use the italics displayed on the webpage: "First name :"
# Text box
Ie. textboxset ('username', username)

Ie. textboxset ('Password ','*****')
Ie. textboxset ('cpassword ','*****')

# Select the drop-down menu:

Ie. listboxselect ('Question ', Unicode ('My User-Defined question', 'cp936') # note that encoding conversion cp936 can be changed to GBK.
Ie. textboxset ('myquery', 'wodewentishishenme ')
Ie. textboxset ('answer', 'wodoubuxihuan ')
Ie. textboxset ('Year', '20140901 ')
Ie. listboxselect ('month', '6 ')
Ie. listboxselect ('day', '22 ')
# Square selection box:

# Ie. checkboxset ('cxk', 1) #1 2 3 or * are all "checked" meaning ''Null String indicates unselected

# Circle selection box: the names of these elements are easy to know when radio has been designed for Web pages.
# Select jazz here
Ie. radiobuttonset ("gender", "1") # Music is the name of the selection list, which is found in the source code of the webpage.

# Final submit button

Print "Enter the verification code and submit"
# Ie. buttonclick ("Submit") # It is much easier to replace mouse clicks

I = 0
While I <100:
Pass
I = I + 1
If true = ie. findtext (Unicode ('congratulation ', 'gbk ')):
Break

Fp = open('163zhanghao.txt ', 'A ')
FP. Write (zhanghao + '\ n ')
FP. Close ()
Ie. Quit ()

Loop Registration

# Coding: GBK
Import cpamie
Import time
Import random
Username = ''. Join (random. Sample ('abcdefghijklmnopqrstuvwxyz0123456789 ', 8 ))
Password = 'wokao1'
Zhanghao = '|'. Join ([username, password])
I = 0
While I <1000:
Ie = cpamie. pamie ()
Ie. navigate ('https: // passport.baidu.com /? Reg ')
Ie. textboxset ('username', username)
Ie. textboxset ('loginpass ','*****')
Ie. textboxset ('verifypass ','*****')
Ie. radiobuttonset ("sex", "1 ")
Ie. textboxset ("email", username + '@ msn.cn ')
I = 0
While I <1000:
Pass
I = I + 1
If true = ie. findtext (Unicode ('congratulation ', 'gbk ')):
Break
Fp = open('163zhanghao.txt ', 'A ')
FP. Write (zhanghao + '\ n ')
FP. Close ()
Ie. Quit ()
I = I + 1

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.