python+selenium:数据参数化(从文件读取数据)

 

#!/usr/bin/env python
# -*- coding=utf-8 -*-
# coding: utf-8


from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from common.read_txt import read_txt
# from common.read_csv import read_csv
from common.read_xlsx import read_xlsx


def get_ele_times(driver, times, func):
    return WebDriverWait(driver, times).until(func)


def open_browser():
    """
    打开浏览器驱动
    :return: 返回句柄
    """
    driver = webdriver.Chrome()
    return driver


def open_url(driver, url):
    """
    打开网址
    :return:
    """
    driver.get(url)
    driver.maximize_window()


def find_element(driver, kwargs):
    """
    :param driver:浏览器句柄, kwargs必须是字典
    1:login_text:登录按钮,             //*[@id="head"]/div[1]/div[3]/a[7]
    2:user_login_button:用户名登录按钮,    //*[text()='用户名登录']
    3:user_id:用户名,              //*[@name=""userName""]
    4:pwd_id:密码,                 //*[@name="password"]
    5:login_id:立即登录按钮,        //*[@id="TANGRAM__PSP_10__submit"]
    :return:login_text, user_login_button, user_id, pwd_id, login_id
    """
    get_ele_times(driver, 3, lambda d: d.find_element_by_xpath(kwargs['login_text'])).click()
    get_ele_times(driver, 5, lambda d: d.find_element_by_xpath(kwargs['user_login_button'])).click()
    user_id = driver.find_element_by_xpath(kwargs['user_id'])
    pwd_id = driver.find_element_by_xpath(kwargs['pwd_id'])
    login_id = driver.find_element_by_xpath(kwargs['login_id'])
    ele_tuple = (user_id, pwd_id, login_id)
    return ele_tuple


def send_values(ele_tuple, account_dict):
    """
    :ele_tuple: 元素对象名称组成的元组
    :account_dict : 用户要输入的值组成的字典
    :return:
    """
    list_key = ['user_name', 'pwd']
    i = 0
    for key in list_key:
        ele_tuple[i].clear()
        ele_tuple[i].send_keys(account_dict[key])
        i = i + 1
    ele_tuple[-1].click()


def login_test(ele_dict, account_dict):
    """
    :param ele_dict:页面元素xpath值组成的一个字典
    :return:
    """
    driver = open_browser()
    url = 'https://www.baidu.com/'
    open_url(driver, url)
    ele_tuple = find_element(driver, ele_dict)
    send_values(ele_tuple, account_dict)
    driver.quit()


if __name__ == '__main__':
    """
    login_text0 = '//*[@id="head"]/div[1]/div[3]/a[7]'
    user_login_button0 = '//*[@id="TANGRAM__PSP_10__footerULoginBtn"]'
    user_id0 = '//*[@name="userName"]'
    pwd_id0 = '//*[@name="password"]'
    login_id0 = '//*[@id="TANGRAM__PSP_10__submit"]'
    ele_dict0 = {'login_text': login_text0, 'user_login_button': user_login_button0,
                 'user_id': user_id0, 'pwd_id': pwd_id0, 'login_id': login_id0}
    user_name0 = 'aaa'
    pwd0 = '123'
    account_dict0 = {'user_name': user_name0, 'pwd': pwd0}

    """
    ele_dict0 = read_txt('..\\test_file\\element.txt')
    print(ele_dict0)

    xlsx_dict = read_xlsx(path='..\\test_file\\send_values.xlsx', sheet='user_pwd')
    for account in xlsx_dict:
        print(account)
        login_test(ele_dict0, account)




 python+selenium:数据参数化(从文件读取数据)

 

python+selenium:数据参数化(从文件读取数据)python+selenium:数据参数化(从文件读取数据) 放开那只大熊猫 发布了52 篇原创文章 · 获赞 13 · 访问量 2万+ 私信 关注
上一篇:pycharm中想自动获取到参数属性的智能提示吗,小技巧,参数类型声明


下一篇:使用jsplumb绘图,实现连线