Purpose: When the program runs errors, you can know how to know more intuitively through the screenshot file
body is wrong, and the screenshot of the window refers to saving errors to the specified location.
Loading method: Driver.get_screnshot_as_file (FILENAME)
The file name of the
screenshot must end with PNG
FILENAME’s file directory must be created manually
# # # import time from selenium import webdriver from selenium.webdriver.common.by import By # Website instantiation driver = webdriver.Chrome() driver.maximize_window() driver.implicitly_wait(10) driver.get("Website Path") try: # Input username driver.find_element(By.ID,"userA").send_keys("admin") # Input password driver.find_element(By.ID,"padd").send_keys("123456") except Exception as e: driver.get_screenshot_as_file("img/error.png") raise e time.sleep(3) driver.quit()
2.1 What is the verification code?
Verification code refers to a randomly generated information (numbers, letters, Chinese characters, pictures, arithmetic questions), etc. In order to prevent malicious request behaviors and increase the security of the application. It is also necessary to register or log in during automation, so the verification code needs to be processed.
2.2 Verification Code processing method
1. Remove the verification code by the development operation and use it in the test environment
2. Set up a universal verification code by development operation, and generally only use in the test environment.
3. Verification code recognition technology is difficult to reach 100%due to the difficulty of technology. It is generally not recommended to use
4. Record Cookie: Skip the login operation by recording cookie.
2.3 Cookie Principle
Cookie is generated by the web server and saved on the user browser. It can include user -related information.
cookie data format: key value pair (dictionary in python)
COOKIE: The client request the server. If the server needs to record the user status, a Cookie data is issued to the client browser
Cookie: When the browser requested the website again, the browser submits the request data with the Cookie data to the server, and the server checks the cookie to identify the user status.
2.4 Selenium operation cookie
Driver.get_cookie (name) Get the cookie information referring to the name of the name returns a dictionary
Driver.get_cookies () Get all cookie information, and return a list
Driver.add_cookie (Dict_Cookie) to the browser -driven to add cookies, dict_cookie is a dictionary
Note: How to confirm which cookie is used to control the user’s permissions:
1. You can ask the development classmates
2. Comparison of cookies before and after login, can be used to control user permissions than before login.
3. Can not exit after logging in, and after exiting, it is equivalent to cookies.
- Write a function that generates a mobile phone number through the Python code (it will generate more junk data)
- Before registering, backup a database (only need to be done once), execute the registered use case, and then restore the database after each execution is performed (if the amount of database database is large, then the recovery time will be relatively long, the efficiency will be efficient, efficiency Compared)
- Through the Python script directly to delete new users in the database (to be familiar with the database business at the bottom, you must know what the registered users are in the table)
import random import time from selenium import webdriver from selenium.webdriver.common.by import By # Define the function name of obtaining the mobile phone number def get_mobile(): mobiles = ['130', '131', '134', '135'] # Determine the format of the mobile phone number number = str(int(time.time()))[2:] # The latter 8 digits of the mobile phone number through the timestamp (must not be repeated) mobile = random.choice(mobiles)+number # The three digits of the handle number format are added with the latter 8 digits return mobile driver = webdriver.Chrome() driver.maximize_window() driver.implicitly_wait(10) driver.get("Website Path") driver.find_element(By.ID,"username").send_keys(get_mobile()) driver.find_element(By.NAME,"verify_code").send_keys("8888") driver.find_element(By.ID,"password").send_keys("123456") driver.find_element(By.ID,"password2").send_keys("123456") driver.find_element(By.CSS_SELECTOR,".J_btn_agree").click() time.sleep(10) driver.quit()
1. Install Autoit V3 tool. AutoIT V3 tool is used to view Windows window information and control information.
2. Install the Pyautoit module.
- Press Pyautoit-Master.zip file
- After entering the decompression directory in the Windows window, enter CMD in the address bar
- Perform the following python setup.py Install
import time import autoit from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.maximize_window() driver.implicitly_wait(10) driver.get("http://www.baidu.com") # Click the camera button above Baidu driver.find_element(By.CSS_SELECTOR, ".soutu_btn").click() # Click to select file ele = driver.find_element(By.CSS_SELECTOR, ".upload_pic") # Define the mouse object action = ActionChains(driver) # Call the mouse event action.click(ele) # 5 5 Mouse event method action.perform() time.sleep(2) # For Windows operation # Get the pop -up window by AutoIT autoit.win_wait_active("Open", 3) # 3 indicates time, second isunit# In the file select the address and file name of the file in the input box autoit.control_send("Open", "Edit1",r"C:\Users\LiaoFei\Pictures\Saved Pictures\333.jpg") # Click the opening button in the pop -up window autoit.control_click("Open", "Button1") time.sleep(10) driver.quit()