The selenium tool supports multiple browsers. There are many online materials. After comparison, I chose the splinter module, because the use of splinter to develop browser automation operations, code writing is relatively simple.
1. Install splinter
Splinter must use cython, lxml, and selenium. Therefore, install the SDK in advance before installation.
Cython, lxml, selenium. The following url is provided:
1) http://download.csdn.net/detail/feisan/4301293
2) http://code.google.com/p/pythonxy/wiki/AdditionalPlugins#Installation_no
3) http://pypi.python.org/pypi/selenium/2.25.0#downloads
4) http://splinter.cobrateam.info/
Note: After selenium is installed, download the driver file of the corresponding browser. It is best to put it together with the location of the browser, and then set the path to the environment variable of windows.
from selenium import webdriver
browser = webdriver.Chrome() # Get local session of Chrome
# If the browser can be started, the setting is successful.
Ii. Use of splinter
Here, I will show the case of automatically logging on to the 126 mailbox. The difficulty is to find the account, password, and logon page elements on the page. Here, you need to view the source code of the 126 mailbox logon page to find the ID of the related control.
For example, enter the password. The Password text Control ID is pwdinput. You can use browser. find_by_id () to locate the Password text box,
Use the fill () method and enter the password. To simulate clicking a button, you must first find the ID of the button control and then use the click () method.
Since the code is relatively simple, I will only give annotations in the code to illustrate the working principle.
(Test environment win7 + python2.7.3 + chrome)
1)Code
----------------------------------------
# Coding = UTF-8
Import time
From splinter import Browser
Def SPLINTER (URL ):
Browser
= Browser ('chrome ')
# Login
126 email websize
Browser. Visit (URL)
# Wait
Web element Loading
Time. Sleep (5)
# Fill
In account and password
Browser. find_by_id ('idinput'). Fill ('xxxxxx ')
Browser. find_by_id ('pwdinput'). Fill ('xxxxx ')
# Click
The button of login
Browser. find_by_id ('loginbtn '). Click ()
Time. Sleep (8)
# Close
The window of brower
Browser. Quit ()
If _ name _ = '_ main __':
Websize3
= 'HTTP: // www.126.com'
Splinter (websize3)
-------------------------------------------