# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
import xlrd
import xlwt
from datetime import date,datetime
class TEST(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(3)
self.base_url = "https://asdw.easyeda.com/admin/orders/all"
self.verificationErrors = []
self.accept_next_alert = True
def test_(self):
str=[]
fname = "dmin.xls"
bk = xlrd.open_workbook(fname)
shxrange = range(bk.nsheets)
try:
sh = bk.sheet_by_name("Sheet1")
except:
print("no sheet in %s named Sheet1" % fname)
#获取行数
nrows = sh.nrows
#获取列数
ncols = sh.ncols
print("nrows %d, ncols %d" % (nrows,ncols))
# 获取第一行第一列数据
cell_value = sh.cell_value(1,0)
print(cell_value)
#print cell_value
# 创建一个Workbook对象,这就相当于创建了一个Excel文件
book = xlwt.Workbook(encoding='utf-8', style_compression=0)
'''
Workbook类初始化时有encoding和style_compression参数
encoding:设置字符编码,一般要这样设置:w = Workbook(encoding='utf-8'),就可以在excel中输出中文了。
默认是ascii。当然要记得在文件头部添加:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
style_compression:表示是否压缩,不常用。
'''
#创建一个sheet对象,一个sheet对象对应Excel文件中的一张表格。
# 在电脑桌面右键新建一个Excel文件,其中就包含sheet1,sheet2,sheet3三张表
sheet = book.add_sheet('test', cell_overwrite_ok=True)
# 其中的test是这张表的名字,cell_overwrite_ok,表示是否可以覆盖单元格,其实是Worksheet实例化的一个参数,默认值是False
# 向表test中添加数据
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_name("email").send_keys("username")
driver.find_element_by_name("password").send_keys("password")
driver.find_element_by_xpath("//input[@class='btn btn-success']").click()
time.sleep(1)
driver.find_element_by_xpath("//li//span[contains(text(),'订单管理')]").click()
driver.find_element_by_xpath("//ul[@class='sub-menu collapse in']//a[@href='https://asdw.easyeda.com/admin/orders/all']").click()
row_list=[]
for i in range(0,nrows):
row_data = sh.row_values(i,0)
row_list.append(row_data)
driver.find_element_by_xpath("//input[@class='form-control input-md']").clear()
driver.find_element_by_xpath("//input[@class='form-control input-md']").send_keys(row_data[0])
driver.find_element_by_xpath("//input[@class='form-control input-md']").send_keys(Keys.ENTER)
time.sleep(2)
# for str in str:
# driver.find_element_by_xpath("//input[@class='form-control input-md']").clear()
# driver.find_element_by_xpath("//input[@class='form-control input-md']").send_keys(row_data[0])
# driver.find_element_by_xpath("//input[@class='form-control input-md']").send_keys(Keys.ENTER)
# driver.implicitly_wait(3)
# time.sleep(2)
try:
string=driver.find_element_by_xpath("//span[contains(@id,'cell_status')]").text
if string == "已发货" or string =="已收件" or string =="可追踪":
sheet.write(i, 0,row_data[0]+string) # 其中的'0-行, 0-列'指定表中的单元,'row_data[0]'是向该单元写入的内容
print(row_data[0] +" "+string)
driver.find_element_by_xpath("//input[@class='form-control input-md']").clear()
except Exception as e:
print(row_data[0]+" 异常")
#保存文件
book.save(r'e:\test1.xls')
if __name__ == "__main__":
unittest.main()