研究了半天
寫了一個自動註冊帳戶的程式
如下
首先會讀取chromedriver
開啟後轉到註冊頁面http://127.0.0.1:8000/accounts/signup/
用find_element_by_id去抓element
再用變數輸入值進去
import json
from pathlib import Path
from time import sleep
import os
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
file_path = Path(__file__).parent
chrome_path = f'{file_path}/driver/chromedriver.exe'
driver = webdriver.Chrome(
executable_path=chrome_path,
)
driver.maximize_window()
for id in range(31,50):
driver.get('http://127.0.0.1:8000/accounts/signup/')
id_email = driver.find_element_by_id('id_email')
id_email.send_keys(f'test_{id}@fox.com')
id_first_name = driver.find_element_by_id('id_first_name')
id_first_name.send_keys('Mark')
id_last_name = driver.find_element_by_id('id_last_name')
id_last_name.send_keys('Test')
id_phone_number = driver.find_element_by_id('id_phone_number')
id_phone_number.send_keys('0987654321')
id_password1 = driver.find_element_by_id('id_password1')
id_password1.send_keys('123')
id_password2 = driver.find_element_by_id('id_password2')
id_password2.send_keys('123')
btn = driver.find_element_by_class_name('btn-primary')
btn.click()
sleep(5)