17网站一起做网店质量怎么样,前端开发课程,html界面设计,安的网络网站建设昨天学习了selenium自动化测试工具的入门#xff0c;知道了Selenium是用于自动化控制浏览器做各种操作#xff0c;打开网页#xff0c;点击按钮#xff0c;输入表单等等。
今天学习通过selenium自动化测试工具自动登录某网站用户操作。
第一步#xff1a;确定目标网址
…昨天学习了selenium自动化测试工具的入门知道了Selenium是用于自动化控制浏览器做各种操作打开网页点击按钮输入表单等等。
今天学习通过selenium自动化测试工具自动登录某网站用户操作。
第一步确定目标网址
比如天天基金网站登录页面https://login.1234567.com.cn/login
第二步确定登录表单元素位置
通过谷歌浏览器F12调试功能可以很快的定位页面元素位置这也是开发常用谷歌浏览器的原因吧
比如用户账号输入框位置 通过 F12 调试确定元素位置然后右键--》Copy--》Copy XPath 获得账号输入框位置 //*[idtbname]
在后面写代码操作该元素使用该方法即可 driver.find_element(By.ID, tbname)
依次类推获取密码记住交易账号单选框已阅读单选框登录按钮等等表单元素位置。 第三步编写代码
采用拆分步骤细化功能模块封装方法编写代码便于后续扩展功能模块。
ttjj_webdriver.py:
# -*- coding: UTF-8 -*-
# selenium 自动化测试工具
import time
import random
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By# 打开驱动
def open_driver():try:# 连接浏览器web驱动全局变量global driver# Linux系统下浏览器驱动无界面显示需要设置参数# “–no-sandbox”参数是让Chrome在root权限下跑# “–headless”参数是不用打开图形界面chrome_options Options()# 设为无头模式chrome_options.add_argument(--headless)chrome_options.add_argument(--no-sandbox)chrome_options.add_argument(--disable-gpu)chrome_options.add_argument(--disable-dev-shm-usage)# 连接Chrome浏览器驱动获取驱动driver webdriver.Chrome(chrome_optionschrome_options)# 此步骤很重要设置chrome为开发者模式防止被各大网站识别出来使用了Seleniumoptions Options()# 去掉提示Chrome正收到自动测试软件的控制# options.add_argument(disable-infobars)# 以键值对的形式加入参数,打开浏览器开发者模式# options.add_experimental_option(excludeSwitches, [enable-automation])# 打开浏览器开发者模式# options.add_argument(--auto-open-devtools-for-tabs)driver webdriver.Chrome(chrome_optionsoptions)# driver webdriver.Chrome()print(连接Chrome浏览器驱动)# 浏览器窗口最大化driver.maximize_window()1, 隐式等待方法driver.implicitly_wait(最大等待时间, 单位: 秒)2, 隐式等待作用在规定的时间内等待页面所有元素加载;3,使用场景:在有页面跳转的时候, 可以使用隐式等待。driver.implicitly_wait(3)# 强制等待随机休眠 暂停0-3秒的整数秒时间区间[0,3]time.sleep(random.randint(0, 3))except Exception as e:driver Noneprint(str(e))# 关闭驱动
def close_driver():driver.quit()print(关闭Chrome浏览器驱动)# 检查元素是否存在
def check_element_exists(condition, element):方法名称: 校验判断网页元素是否存在中文注释: 校验判断网页元素是否存在入参:param condition str 网页元素定位条件param element str 网页元素定位坐标出参:返回状态:return 0 失败return 1 成功return 2 异常返回错误码返回错误信息作 者: PandaCode辉创建时间: 2023-09-21使用范例: check_element_exists(id, username)try:if (not type(condition) is str):print(条件参数错误,不是字符串: element)return [0, 111111, 条件参数错误,不是字符串, [None]]if (not type(element) is str):print(元素参数错误,不是字符串: element)return [0, 111112, 元素参数错误,不是字符串, [None]]# 根据条件定位元素if condition class:driver.find_element(By.CLASS_NAME, element)elif condition id:driver.find_element(By.ID, element)elif condition xpath:driver.find_element(By.XPATH, element)return [1, 000000, 判断网页元素成功, [None]]except Exception as e:return [0, 999999, 判断网页元素是否存在异常, str(e), [None]]def pc_ttjj_login(username, password):方法名称: 登录天天基金用户中文注释: 登录天天基金用户入参:param username str 登录用户param password str 登录密码出参:返回状态:return 0 失败或异常return 1 成功返回错误码返回错误信息作 者: PandaCode辉创建时间: 2023-09-21使用范例: [user123,pwd123]try:if (not type(username) is str):return [0, 111111, 登录用户参数类型错误,不为字符串, [None]]if (not type(password) is str):return [0, 111112, 登录密码参数类型错误,不为字符串, [None]]print(开始打开Chrome浏览器驱动)open_driver()print(随机休眠)# 随机休眠 暂停0-2秒的整数秒time.sleep(random.randint(0, 2))print(username: username /password: password)# 登录时请求的urllogin_url https://login.1234567.com.cn/logindriver.get(login_url)print(随机休眠)# 随机休眠 暂停0-2秒的整数秒time.sleep(random.randint(0, 2))# 清空登录框# 通过webdriver对象的find_element_by_xx( ),在selenium的4.0版本中此种用法已经抛弃。# driver.find_element_by_xpath(./*//input[idtbname]).clear()通过webdriver模块中的By以指定方式定位元素导入模块from selenium.webdriver.common.by import Bydriver.find_element(By.ID,username)driver.find_element(By.CLASS_NAME,passwors)driver.find_element(By.TAG_NAME,imputdriver.find_element(By.ID, tbname).clear()print(输入用户名)# 自动填入登录用户名# driver.find_element_by_xpath(./*//input[idtbname]).send_keys(username)driver.find_element(By.ID, tbname).send_keys(username)print(随机休眠)# 随机休眠 暂停0-2秒的整数秒time.sleep(random.randint(0, 2))# 清空密码框driver.find_element(By.ID, tbpwd).clear()print(输入密码)# 自动填入登录密码driver.find_element(By.ID, tbpwd).send_keys(password)print(随机休眠)# 随机休眠 暂停0-2秒的整数秒time.sleep(random.randint(0, 2))# 点击#记住交易帐号driver.find_element(By.ID, tbcook).click()print(点击记住交易帐号)# 点击#同意服务协议driver.find_element(By.ID, protocolCheckbox).click()print(点击同意服务协议)# 点击登录按钮进行登录driver.find_element(By.ID, btn_login).click()print(点击登录按钮)# 等待3秒启动完成driver.implicitly_wait(3)time.sleep(3)print(随机休眠)# 随机休眠 暂停0-2秒的整数秒time.sleep(random.randint(0, 2))# 检查元素是否存在查看持仓明细元素,用来判断是否登录成功check_rsp check_element_exists(id, myassets_hold)if check_rsp[0] 1:print(登录成功)print(开始关闭Chrome浏览器驱动)close_driver()# 返回容器return [1, 000000, 登录成功, [None]]else:return check_rspexcept Exception as e:print(登录账户异常, str(e))print(开始关闭Chrome浏览器驱动)close_driver()return [0, 999999, 登录账户异常, str(e), [None]]# 主方法
if __name__ __main__:username 123456789password password123# 登录用户rst pc_ttjj_login(username, password)第四步运行测试效果 -------------------------------------------end---------------------------------------