python自動化,使用unittest對界面操作,讀取excel表格數據輸入到頁面查詢結果,在把結果保存到另外一張excel中


# -*- 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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM