首页 > 文章列表 > Python自动化测试之登录脚本的实现

Python自动化测试之登录脚本的实现

Python
474 2023-03-17

环境准备

前提已经安装好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定位元素