Web page Automation tutorial based on Python+selenium+chrome

Source: Internet
Author: User
Tags knowledge base

Web page Automation tutorial based on Python+selenium+chrome

Python version: Python2.7

Selenium version: SELENIUM3

Chrome version:

Browser driver (chromedriver) version:

Installation of Python

Python download Link: https://www.python.org/

After the Python installation is complete ctrl+r enter cmd into the command line interface, enter Python and press ENTER, if Python version information is installed successfully

Python installation selenium is divided into online and offline installations.

Offline Installation:

Selenium download Link: Https://pan.baidu.com/s/1fvX-ZUbQNQpLbUV7SEb71A Password: Pnya

Installation steps:

1. Copy the selenium file to the scripts directory of the Python installation directory, enter CMD in the top navigation bar and press ENTER to enter the command line interface.

2. Enter pip install SELENIUM-3.11.0-PY2.PY3-NONE-ANY.WHL to install, may appear the PIP version does not apply the situation, ignores off on the line.

3. Enter the PIP list after installation to see if the installation is successful

Online Installation:

1, ctrl+r into the command line interface

2, enter pip install Selenium, click Enter

3. Complete the download and installation

4. Enter the PIP list to see if the installation was successful

Driver Download:

The driver version is related to the version of Google Chrome, first look at the browser version installed on your PC, and then download the corresponding driver according to the version number, there may be multiple drivers to adapt to the version of the browser, just the next line.

Download Link: http://chromedriver.storage.googleapis.com/index.html

Coding:

1. Create a directory named Atuotest

2. Create a directory named res under autotest and copy the driver into the

3, in the Autotest directory, click the right mouse button to create a new text file, modify the name of "main.py", be sure to change the suffix to "py"

To run the script:

Enter "cmd" into the command line interface in the Autotest directory address bar, enter "Python main.py" and click Enter to start running the script

Python Basics:

Python supports object-oriented programming and process-oriented programming. For simplicity, we're writing about process-oriented programming.

Basic Python Tutorial: http://www.runoob.com/python3/python3-tutorial.html

Code structure visible: Python.jpg

Web Knowledge Base:

HTML: Web page structure

HTML Basics Tutorial: http://www.runoob.com/html/html-tutorial.html

CSS: Web style, such as length, width, background, etc.

JavaScript: Animation effects, page updates

Selenium Basics:

1. Methods for locating page elements

Find_element_by_name () – Find elements through the name of the element

Find_element_by_class_name () – Find by class

Find_element_by_tag_name () – Find by type of element, generally not in this way

Find_element_by_link_text () – Find elements by link address

Find_element_by_partial_link_text ()

Find_element_by_xpath () – Find elements by XPath

Find_element_by_css_selector () – Find elements through CSS styles

, Baidu input box has "id", "Name", "Class" and other properties, so find this input box element can choose one to find

If you want to click on the "video" link, you can find that the link has "href", "Name", "Class" and other attributes, you can find the href by Find_element_by_link_text () to locate the element, you can also use the name, class to locate

2. Operating elements

Clear () – Clears the contents of the element, usually by clearing the data in the input box

Send_keys () – Simulates the key input in the element

Click () – Tap Element

Submit ()--form

Text () – Gets the textual information for the element

Get_attribute (name) – Gets the value of the element Name property

3. Mouse events

Context_click () – Right-click

Double_click () – Double-click the mouse

Web page Automation tutorial based on Python+selenium+chrome

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.