栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python3+selenium使用函数执行登陆(一)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python3+selenium使用函数执行登陆(一)

一说明

     这里只是简单得使用函数去调用页面操作,需要注意的是使用函数的时候格式需要规范.      特别是空格不要用空格键,用【Tab】按钮去控制

二核心代码
# 获取并设置账号
def input_userName():
    wd.find_element(By.XPATH,
                    '/html/body/div[1]/div/div/div[2]/div/div/div/div/div[1]/div/form/div[1]/div/input').send_keys(
        'delete')


# 获取并设置密码
def input_password():
    wd.find_element(By.XPATH,
                    '/html/body/div[1]/div/div/div[2]/div/div/div/div/div[1]/div/form/div[2]/div/input').send_keys(
        '456789')


# 获取并设置验证码
def input_code():
    wd.find_element(By.XPATH,
                    '/html/body/div[1]/div/div/div[2]/div/div/div/div/div[1]/div/form/div[3]/div[1]/input').send_keys(
        '123')


# 获取并点击按钮
def button_click():
    wd.find_element(By.XPATH, '/html/body/div[1]/div/div/div[2]/div/div/div/div/div[1]/div/form/div[4]/button').click()
    # 等5秒
    time.sleep(5)


# 获取并设置断言
def assert_success():
    login_success = wd.find_element(By.XPATH, '/html/body/div[1]/ul[2]/li[3]/a/div').text
    print(login_success)
    assert login_success == 'delete1'


# 等5秒
def sleep():
    time.sleep(3)


input_userName()
input_password()
input_code()
button_click()
assert_success()
三 原始代码
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:workpythonpython_location2chromedriver.exe'))

# 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
wd.get('http://192.168.20.112:8092')

# 等5秒
time.sleep(5)

# 全屏显示
wd.maximize_window()


# 获取并设置账号
def input_userName():
    wd.find_element(By.XPATH,
                    '/html/body/div[1]/div/div/div[2]/div/div/div/div/div[1]/div/form/div[1]/div/input').send_keys(
        'delete')


# 获取并设置密码
def input_password():
    wd.find_element(By.XPATH,
                    '/html/body/div[1]/div/div/div[2]/div/div/div/div/div[1]/div/form/div[2]/div/input').send_keys(
        '456789')


# 获取并设置验证码
def input_code():
    wd.find_element(By.XPATH,
                    '/html/body/div[1]/div/div/div[2]/div/div/div/div/div[1]/div/form/div[3]/div[1]/input').send_keys(
        '123')


# 获取并点击按钮
def button_click():
    wd.find_element(By.XPATH, '/html/body/div[1]/div/div/div[2]/div/div/div/div/div[1]/div/form/div[4]/button').click()
    # 等5秒
    time.sleep(5)


# 获取并设置断言
def assert_success():
    login_success = wd.find_element(By.XPATH, '/html/body/div[1]/ul[2]/li[3]/a/div').text
    print(login_success)
    assert login_success == 'delete1'


# 等5秒
def sleep():
    time.sleep(3)


input_userName()
input_password()
input_code()
button_click()
assert_success()

# 关闭页面
wd.quit()

四执行结果

五目录 学习目录:https://blog.csdn.net/baidu_28767649/article/details/124835467
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/925797.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号