一、環境準備
Python版本:3.4
編輯器:Pycharm
excel文件:導入的excel模板
二、python代碼
由于工作需要,需要每天定時導入相關excel文件進入后臺數據庫,由于導入的邏輯比較復雜,所以決定通過python模擬登陸導入網站,點擊相關功能來實現自動導入。
代碼如下:#!/usr/bin/env python
# coding=utf-8
# import time
from selenium import webdriver
import os
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
# import requests
# from selenium.webdriver.common.keys import Keys
# 自動化操作創研系統的線索導入功能
def ui_auto_operation():
# 模擬登陸
# rep = requests.Session()
browser = webdriver.Firefox()
browser.implicitly_wait(10) # 設置隱性等待,等待10S加載出相關控件再執行之后的操作
browser.maximize_window()
browser.get('http://www.*******.com.cn/****/Login.aspx')
# time.sleep(10) # 強制等待一般只用于測試
# browser.refresh()
# 輸入用戶名
username = browser.find_element_by_xpath('//*[@id="txtUserName"]')
username.clear()
username.send_keys('*******')
print('username input success')
# 輸入密碼
browser.find_element_by_xpath('//*[@id="txtPassword"]').send_keys('******')
print('password input success')
# # 加載驗證碼
# yzm = browser.find_element_by_xpath('/html/body/div[2]/div/div[2]/form/dl[3]/dd/input')
# yzm.send_keys(input('輸入驗證碼:'))
# 點擊登陸
browser.find_element_by_xpath('//*[@id="btnLogin"]').click()
print('login success')
# cookies = browser.get_cookies()
# for cookie in cookies:
# rep.cookies.set(cookie['name'], cookie['value'])
# 爬取對應網頁的數據
browser.current_window_handle
browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[1]/div[8]/div/a/span').click()
# 切換到當前窗口
browser.current_window_handle
# time.sleep(5)
tow_drive = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[1]/div[8]/ul/li[5]/a')
tow_drive.click()
print('turn success')
browser.current_window_handle
# time.sleep(2)
# 切換到iframe框架里面
browser.switch_to.frame(browser.find_element_by_xpath('//*[@id="mainFrame"]'))
# # 輸入框只讀屬性的修改
# js = 'document.getElementById("Text1").removeAttribute("readonly");'
# browser.execute_script(js)
# # 定位并且輸入路徑數據
# receiveStart = browser.find_element_by_xpath('//*[@id="Text1"]')
# receiveStart.clear()
# receiveStart.send_keys('C:\\fakepath\\5096.xls')
# # receiveStart.send_keys(Keys.RETURN)
# 點擊上傳文件按鈕
browser.find_element_by_xpath('//*[@id="btn1"]').click()
# 調用寫好的exe實現上傳,autoup.exe的建立參考下面的網站
# https://www..com/sunjump/p/7268805.html
os.system("C:\\fakepath\\autoup.exe")
# time.sleep(5)
load = browser.find_element_by_xpath('//*[@id="btn_lead"]')
load.click()
try:
# 每隔2s就去掃描彈出框是否存在,總時長是60s,存在就繼續執行之后代碼
WebDriverWait(browser, 60, 2).until(EC.alert_is_present())
# 處理彈出alert框
alert = browser.switch_to.alert
alert.accept()
finally:
browser.close()
# browser.quit()
if __name__ == '__main__':
# @version : 3.4
# @Author : robot_lei
# @Software: PyCharm Community Edition
ui_auto_operation()
三、注意事項
此網站屬于內部使用,不用輸入驗證碼就可以實現登陸,所以操作相對簡單很多。其中遇到的主要問題:
(1)、iframe框架的切換,也就是加載的網頁中有部分代碼存在iframe里面,導致頁面的代碼加載不出來,python無法定位到相關的元素。
(2)、上傳的輸入框不是直接用的input和參數傳入原因,導致不能直接在在上傳的文件框輸入需要導入的文件路徑,需要點擊導入按鈕,然后選擇文件路徑,再點擊上傳按鈕才可以上傳。
(3)、alert彈出框處理,由于導入數據比較多的時候,時間過長會導致alert彈出框還沒彈出,但是代碼就開始操作彈出框了,此時肯定無法定位元素,所以需要等待一段時間定時掃描查看來處理彈出框。
(4)、使用SciTE Script Editor編輯程序時需要在file->Encoding里面選擇編碼,一般是utf-8。
對應代碼如下:
;等待5秒鐘,讓上傳窗口出現
WinWait("CLASS:#32770","",5)
;把輸入焦點定位到上傳輸入文本框中,類型為Edit,編號為1,也就是上面獲取到內容
ControlFocus("文件上傳", "","Edit1")
;在文件名那里,輸入需要上傳的文件絕對路徑
ControlSetText("文件上傳", "", "Edit1", 'C:\fakepath\5096.xls')
;等待上傳時間,單位是毫秒 1秒 = 1000 毫秒,文件大的話需要設置長點
Sleep(5000)
;點擊"打開"按鈕,也就是上傳,完成整個上傳過程
ControlClick("文件上傳", "","Button1");