-
Notifications
You must be signed in to change notification settings - Fork 1
/
payment.py
330 lines (269 loc) · 11.9 KB
/
payment.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"""
Test to read the temperature and choose the products
1) Launch Chrome driver
2) Navigate to weathershopper page
3) Read the temperature
4) Click on the desired product depending on the temperature
5) Open the product list page
6) Add the products to the cart based on the given conditions on that page
7) Checkout to cart page
8) Check the items in the cart
9) Click on payment button
10) Enter the card details
6) Close the driver
"""
import time
from selenium import webdriver
# Create an instance of Chrome WebDriver
driver = webdriver.Chrome()
# Maximize the browser window
driver.maximize_window()
# Navigate to LinkedIn page
driver.get("https://weathershopper.pythonanywhere.com/")
#Check the title of the page
if(driver.title =="The best moisturizers in the world!"):
print("Loaded page Successfully")
else:
print("Error Loading Page")
# Get the temperature text input from web page
temp = driver.find_element_by_xpath("//span[@id='temperature']").text
# Filter out and read only the the digits form the string
temp1 = int(''.join(filter(lambda i: i.isdigit(), temp)))
#Check the product heading
def check_page(product,page):
if product == page:
print("Correct Product Heading")
else:
print("Inncorrect Product Heading")
def payment()
# Pause the script to wait for page elements to load
time.sleep(3)
# Condition to check weather to buy Moisturizers or Sunscreens depending on the temperature
# Buy a moisturizer if temperature is less than 19 degree celsius
# Buy a sunscreen if temperature is greather than 43 degree celsius
if temp1<19:
product = 'Moisturizers'
moist_button = driver.find_element_by_xpath("//button[@class='btn btn-primary' and text()='Buy moisturizers']")
moist_button.click()
moist_page = driver.find_element_by_xpath("//h2").text
check_page(product,moist_page)
# Creating a list of all moisturizers
list_moisturizer=driver.find_elements_by_xpath('//div[@class="text-center col-4"]')
# A List of products which are added into the cart
products_added=[]
# The products containing Aloe
print("\n Moisturizers containing Aloe are :\t")
aloe_moist_list=[]
for i in list_moisturizer:
try:
current_element=i.find_element_by_xpath('.//p[contains(text(),"Aloe")]')
price_element=i.find_element_by_xpath('.//p[contains(text(),"Price")]').text
price=int(price_element.strip("Price : Rs. "))
print(" ",current_element.text," : ",price)
aloe_moist_list.append([i,price])
except Exception as e:
continue
# Finding Aloe moisturizer with minimum price
first_min_price=1000
for i,j in aloe_moist_list:
if j<first_min_price:
first_min_price=j
first_product=i
print("Moisturizer containing Aloe added to cart : \n ",first_product.find_element_by_xpath('.//p[contains(text(),"Aloe")]').text, "\n and the ",first_product.find_element_by_xpath('.//p[contains(text(),"Price")]').text)
products_added.append(first_product.find_element_by_xpath('.//p[contains(text(),"Aloe")]').text)
# Adding the first product to the cart to the cart
first_product_add=first_product.find_element_by_xpath('.//button[text()="Add"]')
first_product_add.click()
# Finding moisturizers with almond and thier prices
print("\nMoisturizers with Almond are :\n")
almond_list=[]
for i in list_moisturizer:
try:
current_element=i.find_element_by_xpath('.//p[contains(text(),"Almond")]')
price_element=i.find_element_by_xpath('.//p[contains(text(),"Price")]').text
price=int(price_element.strip("Price : Rs. "))
print(" ",current_element.text," ",price)
almond_list.append([i,price])
except Exception as e:
continue
# Finding the least expensive moisturizer that contains almond
second_min_price=1000
for i,j in almond_list:
if j<second_min_price:
second_min_price=j
second_product=i
print("\nMoisturizer containing Almond added to cart :\n ",second_product.find_element_by_xpath('.//p[contains(text(),"Almond")]').text, "\n and the ",second_product.find_element_by_xpath('.//p[contains(text(),"Price")]').text)
# Adding second product to the cart
products_added.append(second_product.find_element_by_xpath('.//p[contains(text(),"Almond")]').text)
second_product_add=second_product.find_element_by_xpath('.//button[text()="Add"]')
second_product_add.click()
# Clicking on cart button to check if the correct moisturizers are added
cart_button=driver.find_element_by_xpath('//button[@class="thin-text nav-link"]')
cart_button.click()
# Finding products in the cart
cart_items=driver.find_elements_by_xpath('//tbody/tr/td[1]')
cart_products=[]
for element in cart_items:
cart_products.append(element.text)
print("\n Products in the cart are :\n")
print("\n".join(cart_products))
# Pause the script to wait for page elements to load
time.sleep(3)
# Checking if the products are addedcorrectly
if products_added==cart_products:
print("\n\n Successfully added the products")
else:
print("Failed to add products")
# Clicking on payment button
pay_button=driver.find_element_by_xpath('//button[@class="stripe-button-el"]')
pay_button.click()
# Switching to iframe
iframe = driver.find_element_by_name('stripe_checkout_app')
driver.switch_to.frame(iframe)
print("Switched frame")
# Entering email id
email=driver.find_element_by_xpath('//input[@type="email"]')
email_id="[email protected]"
email.send_keys(email_id)
# Entering Card No.
cardno=driver.find_element_by_xpath('//input[@placeholder="Card number"]')
cardno.send_keys("5566778899224433")
# Entering Card Expiry Date
expiry=driver.find_element_by_xpath('//input[@placeholder="MM / YY"]')
expiry.send_keys("0822")
# Entering CVC
cvc=driver.find_element_by_xpath('//input[@placeholder="CVC"]')
cvc.send_keys("863")
#Entering Pin Code
pin_code=driver.find_element_by_xpath('//input[@placeholder="Postcode"]')
pin_code.send_keys("560048")
# Ticking the Checkbox
checkbox=driver.find_element_by_xpath('//a[@class="Checkbox"]')
checkbox.click()
# Entering the Phone No.
phone_no=driver.find_element_by_xpath('//input[@autocomplete="mobile tel"]')
phone_no.send_keys("9740915728")
# Clicking on Submit Button
submit=driver.find_element_by_xpath('//button[@type="submit"]')
submit.click()
# Printing Payement Successful Statement
print("Payment Successfull")
# Exiting iframe
driver.switch_to.default_content()
time.sleep(3)
elif temp1>34:
product = 'Sunscreens'
sun_button = driver.find_element_by_xpath("//button[@class='btn btn-primary' and text()='Buy sunscreens']")
sun_button.click()
sun_page = driver.find_element_by_xpath("//h2").text
check_page(product,sun_page)
# List of all sunscreens
list_sunscreens=driver.find_elements_by_xpath('//div[@class="text-center col-4"]')
# List of products on which are added to cart
products_added=[]
# Finding sunscreens with SPF-50 with their prices
print("\n Products with SPF-50 are :\n")
spf50_list=[]
for i in list_sunscreens:
try:
current_element=i.find_element_by_xpath('.//p[contains(text(),"SPF-50")]')
price_element=i.find_element_by_xpath('.//p[contains(text(),"Price")]').text
price=int(price_element.strip("Price : Rs. "))
print(" ",current_element.text," ",price)
spf50_list.append([i,price])
except Exception as e:
continue
# Finding sunscreen of least price that contains SPF-50
first_min_price=1000
for i,j in spf50_list:
if j<first_min_price:
first_min_price=j
first_product=i
print("\n Product with spf 50 :\n ",first_product.find_element_by_xpath('.//p[contains(text(),"SPF-50")]').text, "\n and the ",first_product.find_element_by_xpath('.//p[contains(text(),"Price")]').text)
products_added.append(first_product.find_element_by_xpath('.//p[contains(text(),"SPF-50")]').text)
# Adding first product to the cart
first_product_add=first_product.find_element_by_xpath('.//button[text()="Add"]')
first_product_add.click()
# Pause the script to wait for page elements to load
time.sleep(3)
# Finding sunscreens with SPF-30 with thier prices
print("\nProducts with SPF-30 are :\n")
spf30_list=[]
for i in list_sunscreens:
try:
current_element=i.find_element_by_xpath('.//p[contains(text(),"SPF-30")]')
price_element=i.find_element_by_xpath('.//p[contains(text(),"Price")]').text
price=int(price_element.strip("Price : Rs. "))
print(" ",current_element.text," ",price)
spf30_list.append([i,price])
except Exception as e:
continue
# Finding sunscreen of least price that contains SPF-30
second_min_price=1000
for i,j in spf30_list:
if j<second_min_price:
second_min_price=j
second_product=i
print("\nProduct with SPF-30 :\n ",second_product.find_element_by_xpath('.//p[contains(text(),"SPF-30")]').text, "\n and the ",second_product.find_element_by_xpath('.//p[contains(text(),"Price")]').text)
# Adding second product to the cart
products_added.append(second_product.find_element_by_xpath('.//p[contains(text(),"SPF-30")]').text)
second_product_add=second_product.find_element_by_xpath('.//button[text()="Add"]')
second_product_add.click()
# Clicking on cart button to check if required sunscreens are added
cart_button=driver.find_element_by_xpath('//button[@class="thin-text nav-link"]')
cart_button.click()
# Pause the script to wait for page elements to load
time.sleep(3)
# Finding products in the cart
cart_items=driver.find_elements_by_xpath('//tbody/tr/td[1]')
cart_products=[]
for element in cart_items:
cart_products.append(element.text)
print("\n Products in the cart are :\n")
print("\n".join(cart_products))
# Checking if the right products are added
if products_added==cart_products:
print("\n Successfully added the products")
else:
print("Failed to add products")
# Clicking on payment button
pay_button=driver.find_element_by_xpath('//button[@class="stripe-button-el"]')
pay_button.click()
# Switching to iframe
iframe = driver.find_element_by_name('stripe_checkout_app')
driver.switch_to.frame(iframe)
print("Switched frame")
# Entering email id
email=driver.find_element_by_xpath('//input[@type="email"]')
email_id="[email protected]"
email.send_keys(email_id)
# Entering Card No.
cardno=driver.find_element_by_xpath('//input[@placeholder="Card number"]')
cardno.send_keys("5566778899224433")
# Entering Card Expiry Date
expiry=driver.find_element_by_xpath('//input[@placeholder="MM / YY"]')
expiry.send_keys("0822")
# Entering CVC
cvc=driver.find_element_by_xpath('//input[@placeholder="CVC"]')
cvc.send_keys("863")
#Entering Pin Code
pin_code=driver.find_element_by_xpath('//input[@placeholder="Postcode"]')
pin_code.send_keys("560048")
# Ticking the Checkbox
checkbox=driver.find_element_by_xpath('//a[@class="Checkbox"]')
checkbox.click()
# Entering the Phone No.
phone_no=driver.find_element_by_xpath('//input[@autocomplete="mobile tel"]')
phone_no.send_keys("9740915728")
# Clicking on Submit Button
submit=driver.find_element_by_xpath('//button[@type="submit"]')
submit.click()
# Printing Payement Successful Statement
print("\n Payment Successfull")
# Exiting iframe
driver.switch_to.default_content()
time.sleep(3)
# Pause the script to wait for page elements to load
time.sleep(3)
# Close the browser
driver.close()