-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebdriver_bot.py
148 lines (132 loc) · 6.23 KB
/
webdriver_bot.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import Select
import time
from datetime import datetime
import json
with open("data/details.json") as f:
data = json.load(f)
with open("data/products.json") as f:
products = json.load(f)
product = products['GIGABYTE_RTX3060_EAGLE_OC']
CHROMEDRIVER_PATH = "chromedriver.exe"
options = Options()
# uncomment these to run completely from terminal
#options.add_argument("--headless")
#options.add_argument("--disable-extensions")
driver = webdriver.Chrome(CHROMEDRIVER_PATH, options=options)
driver.get(product['url'])
def print_to_log(content):
print(content)
f = open("stock_log.txt", "a")
f.write(datetime.now().strftime("%d/%m/%Y %H:%M:%S") + ': ' + content + '\n')
f.close()
def refresh():
driver.delete_all_cookies()
driver.get(product['url'])
checked_out = False
def try_to_checkout():
try:
# straight to checkout
driver.get("https://www.currys.co.uk/app/checkout")
# enter post code
try:
postcode = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='delivery_location']/input")))
postcode.send_keys(data["postcode"])
time.sleep(2)
postcode.send_keys(Keys.ENTER)
except:
pass
# click free delivery
WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//button[contains(text(),'Free')]"))).click()
# enter email
email = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.NAME, "email")))
email.send_keys(data["email"])
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Continue')]"))).click()
# continue as guest
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'Continue as a guest')]"))).click()
# title
WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.NAME, "d-title"))).click()
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(),'Mr.')]"))).click()
# first name
fname = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.NAME, "fname")))
fname.send_keys(data["firstname"])
# surname
lname = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.NAME, "lname")))
lname.send_keys(data["surname"])
# mobile number
mobile = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='addresses']/div[2]/div[5]/div/input")))
mobile.send_keys(data["mobile"])
# postcode
postcode = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.NAME, "zip")))
postcode.send_keys(Keys.CONTROL + "a")
postcode.send_keys(data["postcode"])
# address line 1
address1 = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.NAME, "address")))
address1.send_keys(data["address1"])
# city
city = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.NAME, "city")))
city.send_keys(data["city"])
# submit
city.send_keys(Keys.ENTER)
# continue to enter card details
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(),'Card')]"))).click()
# card num
cardNum = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.NAME, "cardNumber")))
cardNum.send_keys(data["cardnum"])
# cardholder name
cardName = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.NAME, "cardholderName")))
cardName.send_keys(data["cardholdername"])
# card exp month
cardExpMon = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.NAME, "expiryDate.expiryMonth")))
cardExpMon.send_keys(data["cardexpmonth"])
# card exp year
cardExpYr = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.NAME, "expiryDate.expiryYear")))
cardExpYr.send_keys(data["cardexpyear"])
# cvv
cvv = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.NAME, "securityCode")))
cvv.send_keys(data["cvv"])
# SEND THE DOLLA
WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='submitButton']"))).click()
print_to_log('CHECKING OUT...')
try:
WebDriverWait(driver, 45).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(text(),'Card')]")))
print_to_log('COULD NOT CHECKOUT')
refresh()
except:
checked_out = True
except:
print_to_log('EXCEPTION')
refresh()
while not checked_out:
try:
# click off of cookie notice
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='onetrust-accept-btn-handler']"))).click()
price = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='product-actions']/div[2]/div/div/span"))).text
# if price is what Currys says
#price[1:len(price)] == product['price']
# if price is below 450
price = float(price[1:len(price)])
if(price <= 450):
add_to_basket = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='product-actions']/div[4]/div[1]/button")))
# code won't get past here if it's out of stock
print_to_log('IN STOCK')
add_to_basket.click()
# let basket update
time.sleep(2)
try_to_checkout()
else:
print_to_log('INCORRECT PRICE ' + '(' + str(price) + ')')
time.sleep(5)
refresh()
except:
print("OUT OF STOCK")
time.sleep(1)
refresh()
print_to_log('BINGO.')
driver.quit()