web of automated testing - Fundamentals

1, Environmental preparation

1.1 drive installation

The plug-in and operation file used in this article: extract password: 02q3 Baidu cloud

1.1.1 chrome driver

Download address: click here
Under Windows system, pay attention to the correspondence between the version of chrome browser and the version of chromedriver Exe is copied to the root directory of the project, and the absolute path of the driver is added when calling the driver, as shown below:

driver=webdriver.Chrome(os . path . abspath("chromedriver . exe"))

1.1.2 firefox driver
Download address: click here , start firefox.


1.1.3 IE driver
Download address: click here , start IE.

driver = webdriver.Ie(os . path . abspath('IEDriverServer . exe'))

When you call the quit method to close the browser, you need to remove the protection mode in the IE option to close the browser.

Choose one of the three browsers. Take chrome as an example:

# Create file test_web.py
import os
import pytest
from time import sleep

# Absolute path to connect to the browser, one of three
# firefox_path=os.path.abspath('drivers/geckodriver.exe')
# ie_path=os.path.abspath('drivers/IEDriverServer.exe')

# You can select output to check whether the path is correct
# print(chrome_path)

# Create a web address to visit:
url= 'https://www.baidu.com/'

Create a decorator object and open the browser

def open_browser():

	# Add the path to the browser to be executed
    driver = webdriver.Chrome(executable_path=chrome_path,options=option)
    # driver = webdriver.Firefox(executable_path=firefox_path)
    # driver = webdriver.Ie(executable_path=ie_path)
    driver.get(url) # Point to the website visited
    sleep(2) # Sleep for 2s and keep the operation stable
    yield driver # Suspend the site
    driver.quit() # Exit close site

Creating Test Cases

def test_case_01(open_browser):
    print('Start executing test cases')
    driver = open_browser
    print('End of test execution')
    assert True

Call case

if __name__ == '__main__':
1.2 selenium installation
pip install selenium

Call selenium

from selenium import webdriver
1.3 chrome startup parameters (take Chrome browser as an example below)

– headless: no interface operation
– start maximized: window maximized
– incognito: running in stealth mode

def open_browser():
    option = webdriver.ChromeOptions()
    # option.add_argument('--headless') # No interface operation
    option.add_argument('--start-maximized')  # Window maximization
    option.add_argument('--incognito')  # Stealth mode operation

– the prompt "the browser is being controlled by an automated program" is not displayed

option.add_experimental_option('excludeSwitches', ['enable-automation'])

2, Common object positioning methods


2.1 id and name

Take Baidu home page as an example, press F12 to enter the developer mode, click the search bar, and locate the corresponding code as follows:

<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">

Add to use case

def test_case_01(open_browser):
    print('Start executing test cases')
    driver = open_browser
     # Locate the search input box by id
    # Locate the search input box through name:
2.2 tag_name and classname

Take Baidu home page as an example, through tag_name location search input box. Not all location methods can accurately locate page elements. Once a certain location method
If the element cannot be located successfully, you must use another location method

# seek failed

Pass class_name locate search input box

# Successful positioning
2.3 css positioning

CSS: cascading style sheets (English full name: Cascading Style Sheets) is a computer language used to represent file styles such as HTML (an application of Standard General Markup Language) or XML (a subset of Standard General Markup Language). CSS can not only modify the web page statically, but also format the elements of the web page dynamically with various scripting languages.

There are two main ways to obtain css paths. One is to use chrome developer tools to copy, and the other can be obtained directly by manual writing.

2.3.1 tool acquisition

It can be copied through the chrome developer tool. Taking Baidu home page as an example, css positioning search input box


2.3.2 manual preparation and acquisition

Tag and attribute combination: tag name [attribute name = attribute value]
When there are multiple labels, the spaces before the labels are separated
The attribute can be any attribute

In addition to the above, class/id can also be written in a special way. Take Baidu search input box as an example:

   	#  Write through class: tag name The class attribute value in the input tag is s_ipt
    # adopt id Authoring: tag names#id attribute value. The id attribute value in the input tag is kw
    #   class and id Combination, input In label id Value is kw,class Value is s_ipt 1driver.find_element_by_css_selector('input#kw.s_ipt')  
2.4 xpath positioning

XPath: that is, XML Path Language, which is a language used to determine the location of a part in an XML document. It is adopted by developers as a small query language.

There are two ways to get the xpath path:
2.4.1 copy xpath path through developer tools

Take Baidu home page as an example, locate the search input box with xpath, right-click and select 'Copy XPath'

driver.find_element_by_xpath('//*[@id="kw"]').send_keys('selenium') 1

2.4.2 write xpath path manually label only path

There are two kinds of pure label paths: one is absolute path and the other is relative path,
Take Baidu home page input box as an example:

  1. Absolute path: [generally not used unless not found]
  1. Relative path:
//form/span[1]/input combination of labels and attributes

Take Baidu home page input box as an example

Syntax: / / tag name [@ attribute name = attribute value]

    #  Combination of tags and attributes

    # Find the page element whose attribute id value is kw in the input tag
    # Find the page element with attribute id = 'kw'
    # There can also be multiple labels
    #Use the properties of the upper label to help locate
    driver.find_elements_by_xpath('''//span[@class='bg s_ipt_wr quickdelete-wrap']/input''') xpath fuzzy matching

Take Baidu home page search button as an example

   # Starts with: find the page element whose value starts with sub in the type attribute of the input tag
    # ends -with: what to end with (Note: chrome uses XPath version 1.0 and does not support this fuzzy query), find the page element whose attribute value ends in mit in the type attribute in the input tag
    # driver.find_elements_by_xpath("//input[ends-with(@type,'mit')]")
    # contains: find the page element containing the "s_btn" string in the class attribute of the input tag
    driver.find_elements_by_xpath("//input[contains(@class, 's_btn')]")
    # Contains: contains text. Find the page element with "hot search" in the text of div tag
    driver.find_elements_by_xpath("//div[contains(text(), 'hot search')] ")
2.5 link_ text and partial_ link_ Textpositioning

Take Baidu home page as an example

link_ Textlocate text link:


partial_ link_ Textlocate text links through partial text:


3, And Add wait time

3.1 sleep wait: time

Sometimes, in order to ensure the stability of the script, you need to add waiting time and sleep in the script:

import time # Import module file first 
time.sleep(3) # Sleep for 3 seconds
3.2 intelligent wait: implicitly wait

Implicitly waiting for an element to be discovered or a command to complete, this method only needs to be called once per session. sleep method is generally used.


4, Print information

print(driver.title) # Print title 
print(driver.current_url) # Print the current page url

5, Browser operation

5.1 browser maximization

It is equivalent to [option.add_argument('– start maximized') # window maximization operation], and this method is generally not used.

5.2 browser forward and backward

Source code in developer mode 1 [search input box]:

<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">

Source code 2 [search button] in developer mode:

<input type="submit" id="su" value="use Baidu Search" class="bg s_btn">

Run code:

    element = driver.find_element_by_id('kw')
    element.send_keys('my love')  # Enter 'my love' in the input box
    driver.find_element_by_id('su').click()  #Click search
    driver.back()  # Browser Back
    driver.forward()  # Browser forward

6, Operation test object

click # click

send_keys # simulate key input

clear # clears the contents of the input box

Submit # submits a form. When type=submit, it is equivalent to click. This method is not available for all objects

    element = driver.find_element_by_id('kw')
    element.send_keys('my love')  # Input content 'my love' in the input box
    element.clear()  # Clear input
    element.send_keys('python')  # Input content 'python'
    driver.find_element_by_id('su').click()  # Search key
    # driver.find_element_by_id('su').submit()  # Search button, the effect is equivalent to click

Text # get element text content

element = driver.find_elements_by_xpath("//div[contains(text(), 'hot search')] ")
print('got text :',element.text())

Get page element properties

7, Keyboard key usage

Take Baidu home page as an example, the search button uses keys Enter, import related classes first

from selenium.webdriver.common.keys import Keys # Import keys


    # Keys. Use of enter method

8, Right mouse button

The operation of right mouse button elements, take Baidu home page as an example

Import ActionChains

from selenium.webdriver.common.action_chains import ActionChains

Install pyautogui third-party library and support right-click menu element operation

pip install pyautogui

Import this library

import pyautogui # Import pyautogui


    # The operation of right mouse button elements, take Baidu home page as an example

9, Locate a set of elements

Check box HTML as an example, locate and operate a group of elements

# Add path
checkbox_file_path = 'file:///'+os.path.abspath('checkbox.html')
 driver.get(checkbox_file_path)  # The file that calls the path
    # Check box HTML as an example, locate and operate a group of elements
    for input in inputs:
        if input.get_attribute('type') == 'checkbox':

10, Positioning frame

Sometimes you can't locate the login interface directly. At this time, there may be a framework. You can't operate until you locate it in the framework. Take QQ space as an example:

url_1='https://i.qq.com / '# add path
driver.get(url_1)  # Call website
# iframe positioning frame qq space

11, Positioning window

Take the home page of the financial sector as an example, first switch the window and then operate the page elements

url_2='http://www.jrj.com.cn '# add path
    # Positioning switch window
    driver.find_element_by_link_text('Sign in').click()  #Open new window page
    driver.switch_to.window(driver.window_handles[-1])  # -1 indicates the latest window
    driver.find_element_by_id('txtUsername').click()  # Locate the user name input box by id

12, Hierarchical positioning

Take Baidu home page as an example, locate the second item in Baidu hot list through hierarchy

  # Hierarchical positioning
    father = driver.find_element_by_class_name('FYB_RD')  # Navigate to parent element first
    # driver.find_elements_by_xpath('#hotsearch-content-wrapper')
    father.find_elements_by_tag_name('a')[2].click()  # Then locate and operate on the child element

13, Operation file upload

With fileUpload Take HTML file as an example to operate on the input tag

# Add path
fileUpload_file_path = 'file:///'+os.path.abspath('fileUpload.html')
 # Operation file upload
    # sleep(3)

14, Drop down box operation

drop_down.html file as an example:

# Import related libraries
from selenium.webdriver.support.select import Select
dropdown_file_path = 'file:///' + os.path.abspath('drop_down.html')
   # Drop down box operation
    element = driver.find_element_by_name('Xin Qiji')
    Select(element).select_by_visible_text('Drunk in the lantern to see the sword, dream back to the camp.')

15, Prompt box operation

With alert HTML file as an example:

# Add path
allert_file_path = 'file:///'+os.path.abspath('alert.html')
# Open the path
#  There is only one OK button for the operation of the alert prompt box
  # The confirm prompt box operates with two buttons, one is "OK" and the other is "Cancel"
    driver.switch_to.alert.accept() # Click "OK"
    driver.switch_to.alert.dismiss()  # Click "Cancel"
#  Prompt prompt box operation, a text input box, a "OK" button and a "Cancel" button
    # driver.switch_to.alert.dismiss()  # Cancel operation
    driver.switch_to.alert.accept()  # Determine operation

16, Execute JS script

Take Baidu homepage as an example:

# Execute js script
js = 'window . scrollTo(0 , document . body . scrollHeight)'
driver.execute_script(js)  # Scroll to the bottom of the page
js = 'window . scrollTo(0 , document . body . scrollTop=0)'
driver.execute_script(js)  # Scroll to the top

17, Po(page object) layered idea

PO is the abbreviation of Page Object. The mode of separating business process from page element operation can be simply understood as that there is a configuration class under each page. The configuration class is used to maintain page elements or operation methods.

Advantages: improve the maintainability and readability of test cases.


PO mode code, take a landing page as an example
After introducing the idea of layering:

Keywords: Python Selenium

Added by marian on Mon, 07 Feb 2022 14:46:57 +0200