用selenium和schedule定时自动填写学校每日位置统计的金数据问卷
环境配置:
- 查看Chrome浏览器版本:Chrome浏览器 设置-关于Chrome
- 打开 http://npm.taobao.org/mirrors/chromedriver/
下载对应版本的ChromeDriver压缩包,
解压到与代码PY文件保存位置相同的文件夹。
代码部分:
(把名字和学号换成你的;缺点是要一直挂机)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import schedule
import time
from selenium.webdriver.common.keys import Keys
# 问卷自动填写:
def auto():
chromeOptions = webdriver.ChromeOptions()
browser = webdriver.Chrome(chrome_options=chromeOptions)
wait = WebDriverWait(browser, 10)
browser.get('https://jinshuju.net/f/vbG7ws') #问卷网址
# 姓名
name_input = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="root"]/div/form/div[3]/div/div[1]/div/div[2]/div/span/span/input')))
name_input.clear()
name_input.send_keys('姓名')
# 学号
sid_input = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="root"]/div/form/div[3]/div/div[2]/div/div[2]/div/span/span/input')))
sid_input.clear()
sid_input.send_keys('学号')
#如果EC.presence_of_element_located出现timeout exception报错,
#可以换成EC.element_to_be_clickable 试试
# 位置
choice = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="root"]/div/form/div[3]/div/div[3]/div/div[2]/div/span/span/div/button')))
choice.click()
# 日期
date_input = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="root"]/div/form/div[3]/div/div[4]/div/div[2]/div/span/span/span/div/input')))
date_input.click()
date_input = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="root"]/div/form/div[3]/div/div[4]/div/div[2]/div/span/span/div/div/div/div/div/div/div[1]/div/input')))
date_input.clear()
date_input.send_keys(' ')
date_input.send_keys(Keys.ENTER)
# 提交
submit = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="root"]/div/form/div[5]/div[1]/button')))
submit.click()
# 关闭网页
browser.quit()
# 定时功能:
schedule.every().day.at("00:02").do(auto)
while True:
schedule.run_pending()
time.sleep(1)