一、前期準備
1、要寫python首先要搭建python環境
(1)首先從python官網下載下傳python安裝程式,或者可以到我的網盤直接下載下傳python3.70的版本 ,提取密碼vsfr
(2)下載下傳完後就可以安裝了,路徑可以自動選擇
(3)安裝python的運作環境後就可以安裝python的用戶端工具JetBrains PyCharm Community Edition 2018.3.3 x64
上面下載下傳環境是直接可以下載下傳開發工具。
2、自動化所需要的内容
(1)首先下載下傳對應的chrome版本,我這裡下載下傳的是78的版本,下載下傳後直接安裝。
(2)下載下傳chromeDriver驅動的程式,下載下傳後直接複制到谷歌浏覽器安裝目錄的執行路徑即可。
(3)查找chromeDriver和chrome的版本對應表,百度很多,可自行查找。
以上資訊可以到網盤直接下載下傳,提取碼:t3c8
好了,前期工作已經準備完成,開始開發程式了
3、開發python自動化代碼
(1)打開工具引入python環境
(2)導入所需要的依賴包,如圖就是所需要的包,點選+号搜尋後直接安裝即可。
(3)建立webdriver.py檔案,代碼内容如下
# -*- coding:utf-8 -*-
from selenium import webdriver
from time import sleep
import time
import pymysql.cursors
from selenium.webdriver.support.ui import Select
##定義連接配接資料庫
connect = pymysql.Connect(
host='localhost',
port=3306,
user='root',
passwd='root',
db='webdriver',
charset='utf8'
)
##擷取遊标
cursor = connect.cursor()
sql = 'SELECT t.* FROM webdriverdaily t';
cursor.execute(sql)
##循環打開浏覽器進行日報填寫
for row in cursor.fetchall():
print(row)
##擷取浏覽器加載驅動
chrome_driver = 'C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe'
brower = webdriver.Chrome(executable_path=chrome_driver)
##窗體最大化
brower.maximize_window()
##打開位址
brower.get("https://oa.gccloud.com:10551/in/index.php")
##打開頁面後等待2秒再輸入
time.sleep(2)
##輸入使用者名密碼
brower.find_element_by_id("username").send_keys(row[1])
brower.find_element_by_id("password").send_keys(row[2])
##單機登入
brower.find_element_by_id("submitBtn").click()
##開始填寫表單
brower.find_element_by_xpath('//*[@id="mainmenu"]/a[7]').click()
time.sleep(1)
inputValue = brower.find_element_by_xpath('//*[@id="wtime[1]"]').get_attribute('value')
if len(inputValue)>0:
##有值,則檢測是否還有沒寫的日報
brower.find_element_by_xpath('//*[@id="content"]/a[1]').click()
time.sleep(1)
inputValue1 = brower.find_element_by_xpath('//*[@id="wtime[1]"]').get_attribute('value')
if len(inputValue1)>0:
##如果填寫則判斷第三個是否填寫
brower.find_element_by_xpath('//*[@id="content"]/a[2]').click()
time.sleep(1)
inputValue2 = brower.find_element_by_xpath('//*[@id="wtime[1]"]').get_attribute('value')
if len(inputValue2)>0:
print("========很勤快,日報已經全部寫了=======")
brower.quit()
else:
print("================日報正在填寫中。。。==================")
time.sleep(2)
brower.find_element_by_xpath('//*[@id="wtime[1]"]').send_keys(row[3])
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[1]/div/div').click()
time.sleep(1)
brower.find_element_by_xpath(
'//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[1]/div/dl/dd[' + row[4] + ']').click()
time.sleep(1)
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[2]/div/div').click()
time.sleep(1)
brower.find_element_by_xpath(
'//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[2]/div/dl/dd[' + row[5] + ']').click()
time.sleep(1)
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[3]/div/div').click()
time.sleep(1)
brower.find_element_by_xpath(
'//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[3]/div/dl/dd[' + row[6] + ']').click()
time.sleep(1)
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[4]/td[2]/div[1]/div/div').click()
time.sleep(1)
brower.find_element_by_xpath(
'//*[@id="edit_form"]/table/tbody/tr[4]/td[2]/div[1]/div/dl/dd[' + row[7] + ']').click()
time.sleep(1)
brower.find_element_by_id("worktdesc[1]").send_keys(row[8])
time.sleep(1)
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[5]/td[2]/div/div/div').click()
time.sleep(1)
brower.find_element_by_xpath(
'//*[@id="edit_form"]/table/tbody/tr[5]/td[2]/div/div/dl/dd[' + row[9] + ']').click()
time.sleep(1)
brower.find_element_by_id("log[1]").send_keys(row[10])
time.sleep(1)
brower.find_element_by_id("save").click()
time.sleep(2)
##執行完成後關閉浏覽器
brower.quit()
else:
print("================日報正在填寫中。。。==================")
time.sleep(2)
brower.find_element_by_xpath('//*[@id="wtime[1]"]').send_keys(row[3])
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[1]/div/div').click()
time.sleep(1)
brower.find_element_by_xpath(
'//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[1]/div/dl/dd[' + row[4] + ']').click()
time.sleep(1)
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[2]/div/div').click()
time.sleep(1)
brower.find_element_by_xpath(
'//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[2]/div/dl/dd[' + row[5] + ']').click()
time.sleep(1)
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[3]/div/div').click()
time.sleep(1)
brower.find_element_by_xpath(
'//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[3]/div/dl/dd[' + row[6] + ']').click()
time.sleep(1)
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[4]/td[2]/div[1]/div/div').click()
time.sleep(1)
brower.find_element_by_xpath(
'//*[@id="edit_form"]/table/tbody/tr[4]/td[2]/div[1]/div/dl/dd[' + row[7] + ']').click()
time.sleep(1)
brower.find_element_by_id("worktdesc[1]").send_keys(row[8])
time.sleep(1)
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[5]/td[2]/div/div/div').click()
time.sleep(1)
brower.find_element_by_xpath(
'//*[@id="edit_form"]/table/tbody/tr[5]/td[2]/div/div/dl/dd[' + row[9] + ']').click()
time.sleep(1)
brower.find_element_by_id("log[1]").send_keys(row[10])
time.sleep(1)
brower.find_element_by_id("save").click()
time.sleep(2)
##執行完成後關閉浏覽器
brower.quit()
else:
print("================日報正在填寫中。。。==================")
time.sleep(2)
brower.find_element_by_xpath('//*[@id="wtime[1]"]').send_keys(row[3])
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[1]/div/div').click()
time.sleep(1)
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[1]/div/dl/dd[' + row[4] + ']').click()
time.sleep(1)
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[2]/div/div').click()
time.sleep(1)
brower.find_element_by_xpath(
'//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[2]/div/dl/dd[' + row[5] + ']').click()
time.sleep(1)
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[3]/div/div').click()
time.sleep(1)
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[3]/td[3]/div[3]/div/dl/dd[' + row[6] + ']').click()
time.sleep(1)
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[4]/td[2]/div[1]/div/div').click()
time.sleep(1)
brower.find_element_by_xpath(
'//*[@id="edit_form"]/table/tbody/tr[4]/td[2]/div[1]/div/dl/dd[' + row[7] + ']').click()
time.sleep(1)
brower.find_element_by_id("worktdesc[1]").send_keys(row[8])
time.sleep(1)
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[5]/td[2]/div/div/div').click()
time.sleep(1)
brower.find_element_by_xpath('//*[@id="edit_form"]/table/tbody/tr[5]/td[2]/div/div/dl/dd[' + row[9] + ']').click()
time.sleep(1)
brower.find_element_by_id("log[1]").send_keys(row[10])
time.sleep(1)
brower.find_element_by_id("save").click()
time.sleep(2)
##執行完成後關閉浏覽器
brower.quit()
(4)java中使用定時調用python程式,我這裡用的是xxx-job定時排程系統定時執行
System.out.println("============開始調用python腳本執行自動化=============");
Process proc;
try {
proc = Runtime.getRuntime().exec("F:\\pythonWebdriver\\Scripts\\python.exe F:\\pythonWebdriver\\webdriver.py");// 執行py檔案
//用輸入輸出流來截取結果
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
proc.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
(5)檢視最終結果
到此自動化填寫日報功能完成。