What is Selenium?

Selenium is a browser automation testing framework that is mainly used for automated testing of Web applications. Its main features are as follows: Open source and free; Multi-platform, browser, multi-language support; Good support for Web pages; The API is simple, flexible and easy to use; Support distributed test case execution. Selenium has two versions, Selenium1.0 and 2.0. Selenium1.0 consists of the following parts:

  • Selenium IDE: Is a plug-in embedded in Firefox that enables the record-and-play functionality of the browser.

  • Selenium Grid: An automated testing aid that makes it easy to run multiple test cases in parallel on multiple machines.

  • Selenium RC: Is the core tool of Selenium that supports test scripts written in many different languages. Selenium RC servers serve as proxy servers to access applications for testing purposes. It is mainly divided into the following two parts:

    1. Client: A library used to write test scripts to control Selenium Server.
    2. Server: Controls the behavior of the browser and consists of three parts: Launcher; HttpProxy; The Core.

Knowing the Selenium1.0 family, Selenium2.0 can simply be thought of as adding WebDriver to Selenium RC. So what’s the difference between Selenium RC and WebDriver? RC, like WebDriver, is a set of specifications for manipulating Web pages. Of course, they work differently.

  • Selenium RC runs JavaScript applications in the browser, using the browser’s built-in JavaScript translator to translate and execute Selenese commands (the Selenium command collection).
  • WebDriver directly controls the browser through native browser support or browser extensions. WebDriver is developed for each browser and replaces JavaScript embedded in the Web application under test. Tight integration with the browser enables the creation of more advanced tests, avoiding the limitations imposed by the JavaScript security model. In addition to support from browser vendors, WebDriver also simulates user input using operating system-level calls.

Two, Selenium environment building

  1. Install python

    The python installation is not detailed here. Go to the Python official website to download the corresponding Python version and install it. Pay attention to the configuration of environment variables.

  2. To install Selenium

    Pip3 Install Selenium pip3 install Selenium pip3 install Selenium pip3 install Selenium

  3. Install the Chrome driver. To use Chrome, download the Chrome driver of the corresponding version

  4. Once you have the tools installed, you can test your environment by using the following code

# -*- coding:utf-8 -*-
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://baidu.com")
driver.close()
Copy the code

Browser can normally open Baidu, so the environment installation is normal

Third, the WebDriver API

  1. Positioning elements
  • Locate elements by id attribute (id attribute must be unique in HTML document)

    Find_element_by_id () method

  • Locate elements with the name attribute (the name attribute is in the current page)

    Find_element_by_name () method

  • The class attribute is used to locate elements

    Find_element_by_class_name () method

  • Tags are used to locate elements

    Find_element_by_tag_name () method

  • Elements are located by text information between tag pairs of link elements

    Find_element_by_link_text () method

  • Locate elements with partial link. Similar to link, you can locate elements with partial text information

    Find_element_by_partial_link_text () method

  • Find_element_by_xpath () method

Absolute path to locate: find_element_by_xpath (‘/HTML/body /… ‘)

Find_element_by_xpath (“/ HTML /body/div[2]/div[1]/div/div[1]/div/form/span[1]/input”)

  • Locate elements using CSS

    • Locate the element by the ID attribute

    Text box: find_element_by_css_selector(“#kw”)

    Button: find_element_by_css_selector (” # su “)

    • Locate elements through the class attribute

    Text box: find_element_by_css_selector(“.s_ipt”)

    Find_element_by_css_selector (“.bg s_btn”)

    • Locate elements by other attribute values

    The text box:

    find_element_by_css_selector(“[name = ‘wd’]”)

    find_element_by_css_selector(“[maxlength = 255]”)

    find_element_by_css_selector(“[autocomplete = ‘off’]”)

  1. Control browser

    Control the browser window size:

    driver=webdriver.Firefox()
    # set window size to x, y
    driver.set_window_size(x,y) 
    # maximize window
    driver.maxmize_window() 
    
    Copy the code
  2. Element operation

Once we get the element, we need to do things with it, such as clicking a button, entering text, submitting a form, and so on. Most of the page interaction methods are provided through the WebElement interface. Common ones are:

  • Clear () Clears the text in the text box
  • Send_keys (*value) Simulates key input
  • Click () These methods are simple and easy to use. Submit () is used to submit a form, such as the submit button in the search box. For example, we can use the following code to directly enter the search keyword and submit it with Submit () instead of obtaining the search button and clicking it.

driver.find_element_by_css_selector("#kw").send_keys("selenium2").submit()

There are several common methods:

  • Size () gets the size of the element
  • Text () gets the text of the element
  • Is_displayed () Indicates whether the element is visible
  1. Mouse and keyboard events

Simulation of right mouse, double click, hover, drag and other operations, will use ActionChains class. Selenium.webdriver.com mon. Action_chains. ActionChains (driver) when calling ActionChains method, not executed immediately, but will all the operation according to the order in a queue, When you call the Perform () method, The Times in the queue are executed in sequence.

ActionChains method list:

Click_and_hold (on_element=None) -- Click the left mouse button Without releasing context_click(on_Element =None) double_click(on_element=None) drag_and_drop(source, target) -- Drag to an element and release drag_and_DROP_by_offset (source, xoffset, yoffset) -- drag to a coordinate and release key_down(value, Element =None) -- press a keyboard key key_up(value, Element =None) -- Release a key move_by_offset(xoffset, Move_to_element (to_element) move_TO_element_with_offset (to_element, xoffset, Perform () -- Perform all actions in the chain release(on_Element =None) -- Release the left mouse key send_keys(*keys_to_send) Send_keys_to_element (element, *keys_to_send) -- Sends a key to the specified elementCopy the code
  1. Set the wait time
  • Display wait: – Main classes and methods used: WebDriverWait, uITIL \ Until_NOT, Expected Conditions

  • Implicit wait: – implicitly_wait

  1. Other operations there are some other operations such as: upload and download files, operation cookie, verification code processing and other operations have not been used, used to learn. Proficient in using the above parts, you can write a variety of common scripts, we are getting started in Selenium