Python enables automated browser operations

Source: Internet
Author: User

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)
-------------------------------------------

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.