商城注册
Posted mary2920
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了商城注册相关的知识,希望对你有一定的参考价值。
需求:对商城做注册操作
1 from selenium import webdriver 2 import time 3 4 5 # 对商城做注册和登录操作 6 # 启动浏览器Chrome 7 driver = webdriver.Chrome() 8 9 # 打开商城主页 10 11 driver.get("http://121.42.15.146:9090/mtx/") 12 time.sleep(3) 13 14 # 点注册 15 16 driver.find_element_by_link_text("注册").click() 17 time.sleep(3) 18 19 # 账号注册 20 21 driver.find_element_by_link_text("账号注册").click() 22 time.sleep(1) 23 24 # 输入用户名和密码 25 driver.find_element_by_name("accounts").send_keys("mary2") 26 driver.find_element_by_name("pwd").send_keys("112233") 27 # time.sleep(1) 28 29 # 勾选协议 30 driver.find_element_by_class_name("am-icon-checked").click() 31 time.sleep(3) 32 33 # 点击注册按钮 34 35 print(driver.find_elements_by_css_selector("button[type=‘submit‘]")) 36 #driver.find_elements_by_css_selector("button[class=‘am-btn am-btn-primary am-btn-block am-radius am-btn-sm btn-loading-example‘]")[0].click() 37 driver.find_elements_by_css_selector("button[type=‘submit‘]")[1].click() 38 time.sleep(3)
另一种注册方法:
1 from selenium import webdriver 2 from time import sleep 3 4 5 class Register: 6 username = ‘zzh061‘ 7 password = ‘a123456‘ 8 9 def __init__(self): 10 self.driver = webdriver.Chrome() 11 self.driver.maximize_window() 12 13 def open_test_fan_page(self): 14 self.driver.get("http://121.42.15.146:9090/mtx/") 15 sleep(2) 16 17 def click_zhuce(self): 18 """点击注册链接""" 19 self.driver.find_element_by_link_text(‘注册‘).click() 20 sleep(2) 21 22 def send_keys_text(self): 23 """填写注册信息""" 24 self.driver.find_element_by_name("accounts").send_keys(self.username) # 用户名 25 self.driver.find_element_by_name("pwd").send_keys(self.password) # 密码 26 sleep(2) 27 28 def select_check_box(self): 29 """定位服务协议复选框并勾选""" 30 # el = self.driver.find_elements_by_xpath("//i[@class=‘am-icon-checked‘]") 31 # print(el) 32 # 1 使用xpath定位 33 self.driver.find_elements_by_xpath("//i[@class=‘am-icon-checked‘]")[0].click() 34 sleep(2) 35 36 # 2 使用js脚本进行定位 37 # element = self.driver.find_elements_by_css_selector("input[type=checkbox]")[0] 38 # self.driver.execute_script("$(arguments[0]).click()", element) 39 40 def click_register_button(self): 41 """点击注册按钮""" 42 self.driver.find_element_by_xpath("//form/div[4]/button").click() 43 44 def quit_driver(self): 45 """关闭浏览器""" 46 if "您好" in self.driver.page_source: 47 """判断用户是否注册成功""" 48 print(f"{self.username}注册成功!") 49 else: 50 print("用户已存在,注册失败") 51 sleep(6) 52 self.driver.quit() 53 54 55 # 实例化注册对象 56 reg = Register() 57 # 打开测试网站,并填写注册信息 58 reg.open_test_fan_page() 59 reg.click_zhuce() 60 reg.send_keys_text() 61 reg.select_check_box() 62 reg.click_register_button() 63 # 关闭浏览器对象 64 reg.quit_driver()
另一种登录方法:
1 from selenium import webdriver 2 from time import sleep 3 4 5 class Login: 6 username = ‘zzh06‘ 7 password = ‘a123456‘ 8 9 def __init__(self): 10 self.driver = webdriver.Chrome() 11 self.driver.maximize_window() 12 13 def open_test_fan_page(self): 14 self.driver.get("http://121.42.15.146:9090/mtx/") 15 sleep(2) 16 17 def click_login(self): 18 """点击登录链接,打开登录页面""" 19 self.driver.find_element_by_link_text(‘登录‘).click() 20 sleep(2) 21 22 def input_login_info(self): 23 """输入登录信息""" 24 self.driver.find_element_by_name(‘accounts‘).send_keys(self.username) 25 self.driver.find_element_by_name(‘pwd‘).send_keys(self.password) 26 sleep(2) 27 28 def click_login_button(self): 29 """点击登录按钮""" 30 self.driver.find_element_by_xpath(‘//form/div[3]/button‘).click() 31 sleep(2) 32 33 def click_sign_out(self): 34 """点击登录链接,退出登录""" 35 if f"{self.username},欢迎来到" in self.driver.page_source: 36 """判断用户是否登录成功""" 37 print(f"{self.username}登录成功!") 38 else: 39 print(f"{self.username}没有登录成功") 40 41 self.driver.find_element_by_link_text(‘退出‘).click() 42 sleep(2) 43 44 def quit_driver(self): 45 """关闭浏览器""" 46 sleep(6) 47 self.driver.quit() 48 49 50 login = Login() 51 login.open_test_fan_page() 52 login.click_login() 53 login.input_login_info() 54 login.click_login_button() 55 login.click_sign_out() 56 login.quit_driver()
以上是关于商城注册的主要内容,如果未能解决你的问题,请参考以下文章