Python自动化测试之登录脚本的实现
作者:三千花灯 时间:2022-11-18 07:30:50
环境准备
前提已经安装好python、pycharm,配置了对应的环境变量。
1、安装selenium模块
文件–>设置—>项目:script---->python解释器---->+selenium
2、安装浏览器驱动器
以谷歌浏览器为例
下载地址:https://chromedriver.chromium.org/downloads
(1)先查看谷歌浏览器版本;
(2)下载类似版本号的.zip,解压到pyhton环境目录下
(也可以下载到pycharm下,在环境变量中添加该路径即可)
代码
1、登录代码
from selenium import webdriver #导入slenium的webdriver包
import time #打开页面时,停留时间,可以不用
driver=webdriver.Chrome() #实例化浏览器对象
driver.get("http://192.168.123.15/provincial/#/login")#向浏览器发送网址(URL)
#打印当前title、URL(可以不用)
title=driver.title
print(title)
now_url=driver.current_url
print(now_url)
#通过xpath定位,输入用户名、密码
driver.find_element_by_xpath("//input[@type='text']").send_keys("test01")
driver.find_element_by_xpath("//input[@type='password']").send_keys("test01")
time.sleep(2)
#点击登录按钮
# driver.find_element_by_xpath("//button[@class='el-button el-button--primary']").click()
driver.find_element_by_xpath("//button[@type='button']").click()
time.sleep(3)
#退出浏览器
driver.quit()
2、xpath定位元素标签
定位到账号、密码、登录操作标签
谷歌浏览器---->检查---->elements—>利用最左边的箭头选择标签---->右击复制xpath定位元素
来源:https://blog.csdn.net/belief_009/article/details/129161123
标签:Python,自动化,登录
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何给MD5加上salt随机盐值
2022-01-13 05:32:37
python selenium 获取标签的属性值、内容、状态方法
2021-03-12 23:02:46
Python使用背景差分器实现运动物体检测
2023-06-21 09:37:22
![](https://img.aspxhome.com/file/2023/9/95279_0s.jpg)
mysql 如何使用JSON_EXTRACT() 取json值
2024-01-16 04:26:46
Python使用函数默认值实现函数静态变量的方法
2023-03-11 16:54:46
python爬取代理IP并进行有效的IP测试实现
2021-05-08 15:31:38
![](https://img.aspxhome.com/file/2023/6/135456_0s.png)
Python 多线程知识点总结及实例用法
2022-04-27 13:19:49
Python爬虫包BeautifulSoup异常处理(二)
2021-12-09 21:53:42
在MySQL中为何不建议使用utf8
2024-01-27 07:07:58
![](https://img.aspxhome.com/file/2023/5/126305_0s.jpg)
asp模板解析类模块(支持if,function,loop及解析缓存)
2008-08-11 13:06:00
Python中列表list以及list与数组array的相互转换实现方法
2023-08-24 06:30:15
Javascript:window对象出身何处
2007-08-28 15:16:00
PHP获取一个字符串中间一部分字符的方法
2024-03-08 20:17:39
python内存监控工具memory_profiler和guppy的用法详解
2023-02-17 01:15:18
详解Python小数据池和代码块缓存机制
2023-02-07 06:54:28
MySql批量插入时如何不重复插入数据
2024-01-29 04:42:00
![](https://img.aspxhome.com/file/2023/1/122921_0s.png)
PHP动态页生成静态页的3种常用方法
2024-05-09 09:02:21
微信小程序列表渲染功能之列表下拉刷新及上拉加载的实现方法分析
2024-05-11 09:34:44
![](https://img.aspxhome.com/file/2023/9/126159_0s.png)
SQL Server 2012 sa用户登录错误18456的解决方法
2024-01-21 03:51:29
![](https://img.aspxhome.com/file/2023/1/88241_0s.jpg)
Python中字典的基础介绍及常用操作总结
2022-11-30 14:33:40
![](https://img.aspxhome.com/file/2023/6/65056_0s.png)