-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJd.py
113 lines (103 loc) · 3.72 KB
/
Jd.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# coding:utf-8
import sys
from Tool.Config import Tool_Config
import time
from selenium import webdriver
from pyvirtualdisplay import Display
from selenium.webdriver.common.keys import Keys
class Jd:
__driver = None
__headless = True
__display = None
def __init__(self, headless=True):
if headless == True:
self.__display = Display(visible=0, size=(1366, 768))
self.__display.start()
self.__headless = headless
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--start-maximized")
self.__driver = webdriver.Chrome(chrome_options=options)
def __del__(self):
if self.__headless == True:
self.__display.stop()
self.__driver.quit()
def login(self, name, passwd):
self.__driver.get('https://passport.jd.com/uc/login')
time.sleep(3)
print(self.__driver.title)
self.__driver.find_element_by_link_text("账户登录").click()
time.sleep(1)
self.__driver.find_element_by_id("loginname").send_keys(name)
time.sleep(1)
self.__driver.find_element_by_id("nloginpwd").send_keys(passwd)
time.sleep(1)
self.__driver.find_element_by_id("nloginpwd").send_keys(Keys.ENTER)
time.sleep(3)
def sign(self):
self.__driver.get("http://vip.jd.com/home.html")
time.sleep(3)
print(self.__driver.title)
try:
self.__driver.find_element_by_id("signIn").click()
except Exception as e:
print("signed")
time.sleep(3)
def shop_sign(self):
self.__driver.get("https://bean.jd.com/myJingBean/list")
time.sleep(3)
ele_pages = self.__driver.find_elements_by_class_name("p-item")
for ele_page in ele_pages:
ele_page.click()
time.sleep(1)
try:
self.shop_sign_page()
except Exception as e:
print(repr(e))
def shop_sign_page(self):
ele_btns = self.__driver.find_elements_by_class_name("s-btn")
list_window = self.__driver.current_window_handle
for ele_btn in ele_btns:
try:
ele_btn.click()
except Exception as e:
continue
time.sleep(3)
shop_window = self.__driver.window_handles[1]
self.__driver.switch_to.window(shop_window)
time.sleep(1)
try:
self.__driver.find_element_by_link_text("签到").click()
except Exception as e:
print(repr(e))
self.__driver.switch_to.window(list_window)
continue
time.sleep(3)
try:
self.__driver.find_element_by_link_text("领取并关注").click()
print("有个礼包砸中了")
time.sleep(1)
self.__driver.find_element_by_link_text("签到").click()
time.sleep(3)
except Exception as e:
print(repr(e))
if len(self.__driver.window_handles) >= 3:
sign_window = self.__driver.window_handles[2]
self.__driver.close()
self.__driver.switch_to.window(sign_window)
self.__driver.close()
time.sleep(1)
self.__driver.switch_to.window(list_window)
time.sleep(1)
if __name__ == "__main__":
conf = Tool_Config.get("jd")
headless = False if len(sys.argv) > 1 and sys.argv[1] == "show" else True
jd = Jd(headless)
try:
jd.login(conf["username"], conf["password"])
jd.sign()
jd.shop_sign()
except Exception as e:
print(repr(e))
if headless == True:
del jd