• selenium 数据驱动


    1、数据不规则,使用xml

    2、使用固定的用户名与密码,使用数据字典

    3、搜索关键字,使用数组

    4、大量的数据,使用数据库

    一、类、函数驱动

    do_class.py

    #encoding=utf-8

    from selenium import webdriver
    #调用public文件的Login类里的函数
    #from public_class_data import Login

    driver = webdriver.Firefox()
    driver.implicitly_wait(10)
    driver.get("http://www.126.com")
    class Account(object):
    '''docstring for Account'''

    def __init__(self,username='',password=''):
    self.username = username
    self.password = password

    def do_login_as(user_info):
    self.driver.find_element_by_id("idInput").clear()
    self.driver.find_element_by_id("idInput").send_keys('username')
    self.driver.find_element_by_id("pwdInput").clear()
    self.driver.find_element_by_id("pwdInput").send_keys('password')
    self.driver.find_element_by_id("loginBtn").click()

    #实例化登录信息
    admin = Account(username='admin',password='123')

    guest = Account(username='guest',password='123')

    #调用登录函数
    do_login_as(admin)
    do_login_as(guest)
    #退出
    def logout(self):
    self.driver.find_element_by_link_text("退出").click()
    self.driver.quit()

     二、数组和字典方式的调用

    public_class_data.py

    #coding=utf-8
    class Login():
    def __init__(self):
    self.driver=driver
    def login(self,username,password):
    self.driver.find_element_by_id("idInput").clear()
    self.driver.find_element_by_id("idInput").send_keys(username)
    self.driver.find_element_by_id("pwdInput").clear()
    self.driver.find_element_by_id("pwdInput").send_keys(password)
    self.driver.find_element_by_id("loginBtn").click()

    #退出
    def logout(self):
    self.driver.find_element_by_link_text("退出").click()
    self.driver.quit()

    #coding=utf-8

    #使用数组的方式读取
    from selenium import webdriver

    '''
    file_info = open("baidu.txt",'r')
    values = file_info.readlines()
    file_info.close()
    '''
    shuzu = ['python','selenium','day day up']
    for search in shuzu:
    driver = webdriver.Firefox()
    driver.implicitly_wait(10)
    driver.get("https://www.baidu.com")
    driver.find_element_by_id("kw").clear()
    driver.find_element_by_id("idInput").send_keys('search')
    driver.find_element_by_id("su").click()
    driver.quit()


    #使用数据字典的方式读数据,如用户名和密码
    #encoding=utf-8
    from selenium import webdriver
    from public_class_data import Login
    from time import sleep
    '''
    file_info = open("baidu.txt",'r')
    values = file_info.readlines()
    file_info.close()
    '''
    zidian = {'user01':'pass01','user02':'pass02'}

    for k,v in zidian.items():
    driver = webdriver.Firefox()
    driver.implicitly_wait(10)
    driver.get("https://www.126.com")
    Login(driver).login(k,v)
    sleep(1)
    Login(driver).logout()
    prink(k,v)

    三、csv文件驱动

    #encoding=utf-8
    import csv

    my_file = 'user_info1.csv'
    data = csv.reader(open(my_file,'r'))
    #data2 = data.encode('gbk','ignore').decode("gbk")

    for user in data:
    print (user)
    #显示其中的某一列
    print (user[0])
    print (user[1])
    print (user[2])
    print (user[3])

    注意:Python3,读文件只能用r,不能用rb,否则会报错:

    Traceback (most recent call last):

      File "D:Python3studydo_data_file_csv.py", line 11, in <module>

        for user in data:

    _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

    [Finished in 0.5s with exit code 1]

    四、txt文件驱动

    #coding=utf-8
    from selenium import webdriver
    from public_class_data import Login
    from time import sleep

    user_file = open("user_info.txt",'r')
    values = user_file.readlines()
    user_file.close()

    #ctrl +d ,批量修改,按几次就选中几个,#分隔行为;
    for i in values:
    username= i.split(';')[0]
    print (username)
    password = i.split(';')[1] #1代表读行里的第二列数据
    print (password)

     五、xml驱动

      

  • 相关阅读:
    程序员代码面试指南:IT名企算法与数据结构题目最优解
    经典排序算法
    Log4j输出格式控制--log4j的PatternLayout参数含义
    常用数据库4 mongodb
    常用数据库2 sqlite及SQL注入
    面试常问-数据库索引实现原理
    自定义web框架
    HTML|CSS之布局相关总结
    C++模板类练习题
    C++中的运算符重载练习题
  • 原文地址:https://www.cnblogs.com/bzdmz/p/10331266.html
Copyright © 2020-2023  润新知