Selenium test framework batch registration

Posted by fri on Tue, 07 Dec 2021 14:09:46 +0100

Selenium test framework

Open cmd to download Selenium first:

pip install -i https://pypi.douban.com/simple selenium

If it is downloaded with cmd: pip show selenium. Is the download successful using this command

Or directly in the pycham IDE

Introduce this shelf package on the project header: from selenium import webdriver

Install browser driver:

I use Google browser here:

When downloading Google's driver, you should pay attention to the version number of your browser to download the corresponding driver

After downloading, you need to add the entire PATH to the system variable PATH:

System Properties > environment variables > Path > double click in > add your own path

I encountered several more pits:

I'll fill it out here

Create a file and:

driver = webdriver.Chrome()

Click Run to see if the browser can start

If you want to open Baidu:

driver = webdriver.Chrome()
#Open Baidu
driver.get("http://baidu.com")
#Close browser
driver.close()

But sometimes an error may be reported at the beginning: the browser driver cannot be found

Solution:

Add the driver path directly in Chrome. The format is as follows:

driver = webdriver.Chrome('Your download path')

After dealing with the above, we can simply write this example

Batch registration:

Website address: http://tpshop-test.itheima.net

Demand ideas:

  • Directly open the registration page of the website
  • Fill in the mobile phone number, verification code and password and click Register
  • Then add a loop to realize batch registration

Implementation of opening the registration page of the website:

use http://tpshop-test.itheima.net/Home/user/reg.html You can directly open the registration interface

# Open browser
driver = webdriver.Chrome()
url='http://tpshop-test.itheima.net/Home/user/reg.html'
# Open this site
driver.get(url)

Run to this step, click Run to open the website

Then fill in the information: you can check find_element this method

#Locate by ID tag
ID = "id"

#Find and locate through xpath Tags
XPATH = "xpath"

#Via LINK_TEXT label find location
LINK_TEXT = "link text"

#Through PARTIAL_LINK_TEXT label find location
PARTIAL_LINK_TEXT = "partial link text"

#Locate by NAME tag
NAME = "name"

#Via TAG_NAME tag find location
TAG_NAME = "tag name"

#Locate by CLASS tag
CLASS_NAME = "class name"

#Through CSS_SELECTOR label lookup positioning
CSS_SELECTOR = "css selector"

We use the location of each element to achieve click and input. Here we use the name and id elements

# Enter the user name by locating the location of the search box by id
driver.find_element(By.ID, 'username').send_keys("13456789000")
# Enter the location of the search box by name
driver.find_element(By.NAME, 'verify_code').send_keys("8888")
# Enter the password by locating the location of the search box by id
driver.find_element(By.ID, 'password').send_keys("123456")
# Enter the password by locating the location of the search box by id
driver.find_element(By.ID, 'password2').send_keys("123456")
# Locate the search button by class
driver.find_element(By.CLASS_NAME, 'J_btn_agree').click()
# Interval 2 seconds
time.sleep(2)
# Close browser
driver.close()

In this way, we successfully registered an account

Now let's implement batch registration according to the cycle, and then generate a. txt file to save the account and password we just generated

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
# Open browser
driver = webdriver.Chrome()
url='http://tpshop-test.itheima.net/Home/user/reg.html'
# Open this site
driver.get(url)
# Loop variable initialization
i = 1
j = 0
sum = []
# Number of generated accounts
while i<10:
    # This place is to change the format of mobile phone number
    number="1340000022" + str(i)
    if (format(driver.current_url) != url):
        driver.find_element(By.LINK_TEXT,'Safe exit').click()
        driver.find_element(By.LINK_TEXT,'register').click()
        driver.refresh()
        time.sleep(3)

    # Enter the user name by locating the location of the search box by id
    driver.find_element(By.ID, 'username').send_keys(number)
    sum.append(number)
    # Enter the location of the search box by name
    driver.find_element(By.NAME, 'verify_code').send_keys("8888")
    # Enter the password by locating the location of the search box by id
    driver.find_element(By.ID, 'password').send_keys("123456")
    # Enter the password by locating the location of the search box by id
    driver.find_element(By.ID, 'password2').send_keys("123456")
    # Locate the search button by class
    driver.find_element(By.CLASS_NAME, 'J_btn_agree').click()
    # Interval 2 seconds
    time.sleep(2)

    # Refresh interface
    driver.refresh()
    i += 1;
    j += 1;

# end
driver.close()

#Generate a txt file to save these accounts
with open("username.txt","w") as f:
    f.write("user name"+'\n')
    for sums in sum:
        f.write(sums+'\n')
    f.write("Unified password: 123456")

Topics: Python Selenium chrome