一、
1.通过csv文件批量导入数据
1 from selenium import webdriver
from time import ctime,sleep
import csv
#循环读取每一行每一个字段csv
bid_info = csv.DictReader(open('E:\\script\\python-script\\demo_test_courses.csv','r'))
dict_data = []
for lines in bid_info:
if bid_info.line_num == 1:
continue
else:
dict_data.append(lines)
row_num = len(dict_data)#计算csv文件行数,从而得出i的循环限制数
# print('this is all the data---' + str(dict)) i = 0#初始化循环次数值
while(i < row_num):
#print('this is'+str(i)+'row----'+ str(dict_data[i]))
#print(dict_data[i])
print((dict_data[i])['first_name'])
print((dict_data[i])['last_name'])
#创建课程
driver = webdriver.Chrome()
driver.get("http://demo.pingnanlearning.com/test/login/index.php")
driver.find_element_by_id("username").send_keys("grace")
driver.find_element_by_id("password").send_keys("")
driver.find_element_by_id("loginbtn").click()
driver.find_element_by_link_text(u"系统管理").click()
driver.find_element_by_link_text(u"添加课程").click()
driver.find_element_by_id("").click()
driver.find_element_by_class_name("kc_btn_next").click()
driver.find_element_by_name("fullname").send_keys((dict_data[i])['first_name'])
driver.find_element_by_id("idnumber").send_keys((dict_data[i])['last_name'])
sleep(5)
driver.find_element_by_xpath("//*[@id=\"course2\"]/div[2]/input[2]").click()
sleep(5)
driver.find_element_by_xpath("//*[@id=\"course3\"]/div[3]/input[2]").click()
sleep(5)
#课程开启操作
driver.find_element_by_xpath("//*[@id=\"course4\"]/div[2]/input[3]").click()
# a=driver.find_element_by_class_name("btn_switch").value
# a=1
#创建课程后将课程由默认状态“禁用”变更为“启用”
driver.find_element_by_css_selector("#tip_s2>label").click()
i += 1

对应csv文件所在位置设定如下图:

csv中文件内容如下:

运行成功截图:

2.将上方读取的csv文件内容写入到已经创建好的csv文件中。

 #读取csv文件再讲读取的数据写入csv文件
csvfile2 = open('E:\\script\\python-script\\demo_test_courses-w.csv','w',newline='')
writer=csv.writer(csvfile2)
i=0
writer.writerow(('first_name','last_name'))
while(i<row_num):
print((dict_data[i])['first_name'],(dict_data[i])['last_name'])
writer.writerow(((dict_data[i])['first_name'],(dict_data[i])['last_name']))
i += 1
csvfile2.close()

——————————————————————————————————————————

零散思路记录:

with open("E:\\script\\python-script\\demo_test_courses.csv","r") as csvfile:
    reader=[each for each in csv.DictReader(csvfile)]
for row in reader:
    print(row['first_name'],row['last_name'])
   '''
    reader = csv.reader(csvfile)
    for i,rows in enumerate(csvfile):
        if i==3:
            row=rows
            print(row)

'''
'''    
    rows=[row for row in reader]
    print(rows)
   
    row=csvfile.readline()
    for row in reader:
        for row in reader:
            print(reader.line_num,row[0])
            print(len(csvfile))
'''

二、通过txt文件批量导入数据,读取fullname和idnumber

 txtfile=open("E:\\script\\python-script\\demo_test_a\\demo_test_courses.txt","r")#引用的txt文件
un = txtfile.readline()#读取一行数据
i = 0
new = []#将读取的每一行数据以小数组subject为单元存放在一个大数组new中。
while(i <2):#此处的2随着txt文件中行数的变化去自定义输入
un=un.strip('\n')#去掉读取末尾的回车
subject=un.split(',')#以逗号为分隔符读取数据
new.append(subject)
#print(new[i])
print(subject[0])#fullname读取
print(subject[1])#idnumber读取
un=txtfile.readline()
i+=1
txtfile.close()

放到场景中的代码记录:

 from selenium import webdriver
from time import ctime,sleep
#import csv
import unittest,os,time
#通过txt文件参数化课程名和课程编号
txtfile = open("E:\\script\\python-script\\demo_test_a\\demo_test_courses.txt", "r") # 用户名文件
new = []
un = txtfile.readline() # 读取一行数据
#un = txtfile.readline()
#循环读取每一行
i = 0
while(i <2):
un = un.strip('\n') # 去掉读取末尾的回车
subject = un.split(',') # 以逗号为分隔符读取数据
new.append(subject)
#创建课程
driver = webdriver.Chrome()
driver.get("http://demo.pingnanlearning.com/test/login/index.php")
driver.find_element_by_id("username").send_keys("grace")
driver.find_element_by_id("password").send_keys("")
driver.find_element_by_id("loginbtn").click()
driver.find_element_by_link_text(u"系统管理").click()
driver.find_element_by_link_text(u"添加课程").click()
driver.find_element_by_id("").click()
driver.find_element_by_class_name("kc_btn_next").click()
driver.find_element_by_name("fullname").send_keys(subject[0])
driver.find_element_by_id("idnumber").send_keys(subject[1])
print(subject[0])
print(subject[1])
sleep(5)
driver.find_element_by_xpath("//*[@id=\"course2\"]/div[2]/input[2]").click()
sleep(5)
driver.find_element_by_xpath("//*[@id=\"course3\"]/div[3]/input[2]").click()
sleep(5)
driver.find_element_by_xpath("//*[@id=\"course4\"]/div[2]/input[3]").click()
# a=driver.find_element_by_class_name("btn_switch").value
# a=1
#创建课程后将课程由默认状态“禁用”变更为“启用”
driver.find_element_by_css_selector("#tip_s2>label").click()
un = txtfile.readline()#读取txt文件中的下一行数据
i += 1#控制循环次数+1
txtfile.close()

文件在电脑上的位置及文件基本内容:

三、通过excel文件批量导入数据,读取fullname和idnumber

举例说明excel读取相关知识点:

 from selenium import webdriver
from time import ctime,sleep
import xlrd #通过xls文件参数化课程名和课程编号
xlsfile=r"E:\script\python-script\demo_test_a\demo_test_courses_xls.xls"#打开指定路径中的xls文件
book=xlrd.open_workbook(xlsfile)#得到Excel文件的book对象,实例化对象
sheet0 = book.sheet_by_index(0) # 通过sheet索引获得sheet对象
print("1、",sheet0)
sheet_name = book.sheet_names()[0]# 获得指定索引的sheet表名字
print ("2、",sheet_name)
sheet1 = book.sheet_by_name(sheet_name)# 通过sheet名字来获取,当然如果知道sheet名字就可以直接指定
nrows = sheet0.nrows # 获取行总数
print ("3、",nrows)
#循环打印每一行的内容
for i in range(nrows):
print( sheet1.row_values(i))
ncols = sheet0.ncols #获取列总数
print("4、",ncols)
row_data = sheet0.row_values(0) # 获得第1行的数据列表
print (row_data)
col_data = sheet0.col_values(0) # 获得第1列的数据列表
print("5、",col_data)
# 通过坐标读取表格中的数据
cell_value1 = sheet0.cell_value(0, 0)
print("6、",cell_value1)
cell_value2 = sheet0.cell_value(0, 1)
print ("7、",cell_value2)

对应成功图图和文件夹位置及文件内容:

demo环境通过excel批量创建课程代码及截图展示:

 from selenium import webdriver
from time import ctime,sleep
import xlrd #通过xls文件参数化课程名和课程编号
xlsfile=r"E:\script\python-script\demo_test_a\demo_test_courses_xls.xls"#打开指定路径中的xls文件
book=xlrd.open_workbook(xlsfile)#得到Excel文件的book对象,实例化对象
sheet0 = book.sheet_by_index(0) # 通过sheet索引获得sheet对象
print("1、",sheet0)
sheet_name = book.sheet_names()[0]#aaaaa 获得指定索引的sheet表名字
print ("2、",sheet_name)
sheet1 = book.sheet_by_name(sheet_name)# 通过sheet名字来获取,当然如果知道sheet名字就可以直接指定
nrows = sheet0.nrows # 获取行总数
print ("3、",nrows)
#循环打印每一行的内容
for i in range(nrows):
print( sheet1.row_values(i))
'''
ncols = sheet0.ncols #获取列总数
print("4、",ncols)
row_data = sheet0.row_values(0) # 获得第1行的数据列表
print (row_data)
col_data = sheet0.col_values(0) # 获得第1列的数据列表
print("5、",col_data)
'''
#循环读取每一行
i = 1
while(i < nrows): # 通过坐标读取表格中的数据
cell_value1 = sheet0.cell_value(i, 0)
#print("first_name:", cell_value1)
cell_value2 = sheet0.cell_value(i, 1)
#print("last_name:", cell_value2)
#创建课程
driver = webdriver.Chrome()
driver.get("http://demo.pingnanlearning.com/test/login/index.php")
driver.find_element_by_id("username").send_keys("grace")
driver.find_element_by_id("password").send_keys("")
driver.find_element_by_id("loginbtn").click()
driver.find_element_by_link_text(u"系统管理").click()
driver.find_element_by_link_text(u"添加课程").click()
driver.find_element_by_id("").click()
driver.find_element_by_class_name("kc_btn_next").click()
driver.find_element_by_name("fullname").send_keys(cell_value1)
driver.find_element_by_id("idnumber").send_keys(cell_value2)
sleep(5)
driver.find_element_by_xpath("//*[@id=\"course2\"]/div[2]/input[2]").click()
sleep(5)
driver.find_element_by_xpath("//*[@id=\"course3\"]/div[3]/input[2]").click()
sleep(5)
driver.find_element_by_xpath("//*[@id=\"course4\"]/div[2]/input[3]").click()
# a=driver.find_element_by_class_name("btn_switch").value
# a=1
#创建课程后将课程由默认状态“禁用”变更为“启用”
driver.find_element_by_css_selector("#tip_s2>label").click()
i += 1

对应文件位置及内容截图:

【Python】通过python代码实现demo_test环境的登录,通过csv/txt/excel文件批量添加课程并开启课程操作--(刚开始 项目 页面 模块 元素这种鸟 被称作pageobject 等这些搞完 然后把你的定位器、数据 和脚本在分离 就是传说中那个叫数据驱动 的鸟)的更多相关文章

  1. python读取与写入csv,txt格式文件

    python读取与写入csv,txt格式文件 在数据分析中经常需要从csv格式的文件中存取数据以及将数据写书到csv文件中.将csv文件中的数据直接读取为dict类型和DataFrame是非常方便也很 ...

  2. 「Python实用秘技04」为pdf文件批量添加文字水印

    本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的系列文章「Python实用秘技」的第4期 ...

  3. 第15.25节 PyQt(Python+Qt)入门学习:Model/View开发实战--使用QTableView展示Excel文件内容

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 在前面的订阅专栏<第十九章.Model/View开发:QTableView的功能及属 ...

  4. python操作csv和excel文件

    1.操作csv文件 1).读取文件 import csv f=open("test.csv",'r') t_text=csv.reader(f) for t,i in t_text ...

  5. Python json数据写入csv json excel文件

    一.写入 写入csv和json, 可以使用csv这个包写, 我这里没有使用, 并且把写csv和json的写到一起了 具体的代码就不解释了 def write_file(file_name, items ...

  6. centos7 lvm合并分区脚本初探-linux性能测试 -centos7修改网卡名字-jdk环境安装脚本-关键字查询文件-批量添加用户

    1.#!/bin/bash lvmdiskscan | grep centos > /root/a.txt a=`sed -n '1p' /root/a.txt` b=`sed -n '2p' ...

  7. python读取txt、csv和excel文件

    一.python读取txt文件:(思路:先打开文件,读取文件,最后用for循环输出内容) fp = open('test.txt','r') lines = fp.readlines() fp.clo ...

  8. python基础===Python性能优化的20条建议

    优化算法时间复杂度 算法的时间复杂度对程序的执行效率影响最大,在Python中可以通过选择合适的数据结构来优化时间复杂度,如list和set查找某一个元素的时间复杂度分别是O(n)和O(1).不同的场 ...

  9. Linux程序写入oralce数据库中文显示为问号??? 代码实现设置环境变量!

    Linux程序写入oralce数据库中文显示为问号??? 1.问题介绍 根本原因是字符集的问题,是数据库的字符集和写入程序的linux系统的字符集不一致导致: 但是用export NLS_LANG=& ...

随机推荐

  1. Leetcode: mimimum depth of tree, path sum, path sum II

    思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...

  2. swift开发之 -- ? 和 ! 的作用

    记录下这个知识点: 一般我们在一下两种情况会遇到 ? 和 !的使用 1,声明变量时 var number:Int? var str:String? 2,在对变量进行操作时 number?.hasVal ...

  3. java中定义一个CloneUtil 工具类

    其实所有的java对象都可以具备克隆能力,只是因为在基础类Object中被设定成了一个保留方法(protected),要想真正拥有克隆的能力, 就需要实现Cloneable接口,重写clone方法.通 ...

  4. dos命令临时和永久设置环境变量方法

    方法一:批处理中,修改环境变量,一次性有效(也就是在当前的脚本中有效)   CMD中运行:set path==%path%;d:/mypath   用 set path可以查看,当前的环境变量   方 ...

  5. Mybaits中的update

    <update id="update" parameterType="Currency"> UPDATE YZ_SECURITIES_CURRENC ...

  6. 测试sql语句性能,提高执行效率

    为了让您的程序执行的效率更高,SQL的效率一定不可忽视. 现有以下方法去检测SQL的执行效率. 对于多表查询的效率测试: )直接from ,where方式. SET STATISTICS io ON ...

  7. Ubuntu14.04下安装DevStack

    虚拟机中的网络配置 NET8 为nat net2 为host-only 虚拟机网络配置 # The primary network interface vmnet nat type auto eth0 ...

  8. Dockerfile分享之SSH Server

    版权声明:本文由姚俊刚原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/84 来源:腾云阁 https://www.qclou ...

  9. linux如何设置用户权限

    linux与用户权限设置: 1.添加用户 首先用adduser命令添加一个普通用户,命令如下: #adduser tommy //添加一个名为tommy的用户 #passwd tommy //修改密码 ...

  10. elasticsearch-1.2.1客户端连接DEMO

    1.下载elasticsearch-1.2.1的zip包,解压之后 双击bin目录中的 elasticsearch.bat(针对windows系统) 启动服务器(默认监听9200端口) 访问 http ...