iT邦幫忙

2021 iThome 鐵人賽

DAY 29
0
Modern Web

Python x Django 網站實作&學習記錄系列 第 29

D29 Selenium 自動註冊帳戶

研究了半天
寫了一個自動註冊帳戶的程式
如下
首先會讀取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)

上一篇
D28 Selenium 測試網頁與參數
下一篇
D 30 Python x Django 學習心得
系列文
Python x Django 網站實作&學習記錄30

尚未有邦友留言

立即登入留言