一说明
这里只是将函数封装,然后调用函数.
需要注意的是使用函数的时候格式需要规范,特别是空格不要用空格键,用【Tab】按钮去控制
二 原始代码
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# 创建 WebDriver 对象,指明使用chrome浏览器驱动
from selenium.webdriver.common.by import By
# 加载谷歌浏览器驱动
wd = webdriver.Chrome(service=Service(r'F:\work\python\python_location2\chromedriver.exe'))
# 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
def getUrl(url):
wd.get(url)
# 等5秒
time.sleep(5)
# 全屏显示
wd.maximize_window()
# 定义输入框的函数
def input_text(location_type, value, text):
wd.find_element(by=location_type, value=value).send_keys(text)
# 定义点击按钮的函数
def button_click(location_type, value):
wd.find_element(by=location_type, value=value).click()
time.sleep(5)
# 获取并设置断言
def assert_success(location_type, value, result):
login_success = wd.find_element(by=location_type, value=value).text
print(login_success)
assert result in login_success
# 函数的调用
# 输入登录页面网址:
getUrl('http://192.168.20.112:8092')
# 输入账号
input_text(By.NAME, 'userName', 'delete')
# 输入密码
input_text(By.ID, 'password', '456789')
# 输入验证码
input_text(By.XPATH, '/html/body/div[1]/div/div/div[2]/div/div/div/div/div[1]/div/form/div[3]/div[1]/input', '111')
# 点击登录按钮
button_click(By.XPATH, '/html/body/div[1]/div/div/div[2]/div/div/div/div/div[1]/div/form/div[4]/button')
# 登录成功后的断言验证
assert_success(By.XPATH, '/html/body/div[1]/ul[2]/li[3]/a/div', 'delete1')
# 关闭页面
wd.quit()