forked from jumpingchu/PChome-AutoBuy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpchome_autobuy.py
114 lines (91 loc) · 3.91 KB
/
pchome_autobuy.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
114
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
# 欲搶購的連結、登入帳號、登入密碼及其他個資
from settings import URL, CHROME_PATH, ACC, PWD, BuyerSSN, BirthYear, BirthMonth, BirthDay, multi_CVV2Num
# 設定此 option 可讓 chrome 記住已登入帳戶,成功後可以省去後續"登入帳戶"的程式碼
options = webdriver.ChromeOptions()
options.add_argument(CHROME_PATH)
driver = webdriver.Chrome(
executable_path='chromedriver.exe', chrome_options=options)
driver.set_page_load_timeout(120)
try:
driver.get(URL)
### 放入購物車 ###
WebDriverWait(driver, 20).until(
expected_conditions.element_to_be_clickable(
(By.XPATH, "//li[@id='ButtonContainer']/button"))
)
driver.find_element_by_xpath("//li[@id='ButtonContainer']/button").click()
### 前往購物車 ###
WebDriverWait(driver, 20).until(
expected_conditions.element_to_be_clickable((By.ID, "ico_cart"))
)
driver.find_element_by_id('ico_cart').click()
### 登入帳戶 ###
WebDriverWait(driver, 20).until(
expected_conditions.presence_of_element_located((By.ID, 'loginAcc'))
)
elem = driver.find_element_by_id('loginAcc')
elem.send_keys(ACC)
elem = driver.find_element_by_id('loginPwd')
elem.send_keys(PWD)
WebDriverWait(driver, 20).until(
expected_conditions.element_to_be_clickable((By.ID, "btnLogin"))
)
driver.find_element_by_id('btnLogin').click()
### 前往結帳 (一次付清) ### (要使用 JS 的方式 execute_script 點擊)
WebDriverWait(driver, 20).until(
expected_conditions.element_to_be_clickable(
(By.XPATH, "//li[@class='CC']/a[@class='ui-btn']"))
)
button = driver.find_element_by_xpath(
"//li[@class='CC']/a[@class='ui-btn']")
driver.execute_script("arguments[0].click();", button)
### 填入各項資料 ### (BuyerSSN, BirthYear, BirthMonth, BirthDay, multi_CVV2Num)
WebDriverWait(driver, 20).until(
expected_conditions.element_to_be_clickable(
(By.XPATH, "//input[@id='BuyerSSN']"))
)
elem = driver.find_element_by_xpath("//input[@id='BuyerSSN']")
elem.send_keys(BuyerSSN)
WebDriverWait(driver, 20).until(
expected_conditions.element_to_be_clickable(
(By.XPATH, "//input[@name='BirthYear']"))
)
elem = driver.find_element_by_xpath("//input[@name='BirthYear']")
elem.send_keys(BirthYear)
WebDriverWait(driver, 20).until(
expected_conditions.element_to_be_clickable(
(By.XPATH, "//input[@name='BirthMonth']"))
)
elem = driver.find_element_by_xpath("//input[@name='BirthMonth']")
elem.send_keys(BirthMonth)
WebDriverWait(driver, 20).until(
expected_conditions.element_to_be_clickable(
(By.XPATH, "//input[@name='BirthDay']"))
)
elem = driver.find_element_by_xpath("//input[@name='BirthDay']")
elem.send_keys(BirthDay)
WebDriverWait(driver, 20).until(
expected_conditions.element_to_be_clickable(
(By.XPATH, "//input[@name='multi_CVV2Num']"))
)
elem = driver.find_element_by_xpath("//input[@name='multi_CVV2Num']")
elem.send_keys(multi_CVV2Num)
### 勾選同意 ###
WebDriverWait(driver, 20).until(
expected_conditions.element_to_be_clickable(
(By.XPATH, "//input[@name='chk_agree']"))
)
driver.find_element_by_xpath("//input[@name='chk_agree']").click()
### 送出訂單 ### (要使用 JS 的方式 execute_script 點擊)
WebDriverWait(driver, 20).until(
expected_conditions.element_to_be_clickable(
(By.XPATH, "//a[@id='btnSubmit']"))
)
button = driver.find_element_by_xpath("//a[@id='btnSubmit']")
driver.execute_script("arguments[0].click();", button)
except Exception as e:
print(e)