-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_checkout.py
90 lines (77 loc) · 2.91 KB
/
auto_checkout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ActionChains
USER = ''
PASSWD = ''
LOGIN_URL = 'http://web.oa.zulong.com/C6/JHSoft.Web.Login/OALogin.aspx'
INDEX_URL = 'http://web.oa.zulong.com/C6/jhsoft.web.portal/default/index.aspx'
browser = None
def login(user, password):
browser.get(LOGIN_URL)
browser.find_element_by_id("login_user").send_keys(user)
browser.find_element_by_id("login_pswd").send_keys(password)
browser.find_element_by_id("login_btn").click()
WebDriverWait(browser, 3)
def checkin():
browser.execute_script("DoSignIn();")
WebDriverWait(browser, 10).until(
lambda x: x.find_element_by_class_name('fullscreenmask').is_displayed())
def checkout():
browser.execute_script("DoSignOutCheck();")
WebDriverWait(browser, 10).until(
lambda x: x.find_element_by_class_name('signOutBtn').is_displayed())
def checkout_confirm():
btn_confirm = browser.find_element_by_class_name('signOutBtn')
if btn_confirm.is_displayed():
remain_time = browser.find_element_by_class_name('signMsgBox')
matchObj = re.match(r'.*当前时间:(.*):.*实际工作时长:(.*)小时.*', remain_time.text, re.DOTALL)
if matchObj:
if int(matchObj.group(1)) >= 18 and int(matchObj.group(2)) >= 8:
btn_confirm.click()
WebDriverWait(browser, 10).until(
lambda x: x.find_element_by_class_name('fullscreenmask').is_displayed())
else:
print("It's too early to checkout!!!")
else:
print("You can't checkout now!")
def open_driver():
global browser
#browser = webdriver.PhantomJS()
#browser.set_window_size(1024, 768)
chrome_options = Options()
# 无头模式启动
chrome_options.add_argument('--headless')
# 谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--disable-gpu')
# 初始化实例
browser = webdriver.Chrome(chrome_options=chrome_options)
def run():
login(USER, PASSWD)
browser.get(INDEX_URL)
if browser.find_element_by_class_name('signIn1').is_displayed():
checkin()
elif browser.find_element_by_class_name('signOut1').is_displayed():
checkout()
checkout_confirm()
else:
print("Invalid state!")
def main():
open_driver()
try:
run()
except Exception as err:
print("err: " + str(err))
browser.quit()
if __name__ == '__main__':
if len(sys.argv) < 3:
print("Usage: auto_checkout Username Password")
else:
USER = sys.argv[1]
PASSWD = sys.argv[2]
main()