-
Notifications
You must be signed in to change notification settings - Fork 0
/
shopifytest2.py
163 lines (138 loc) · 4.55 KB
/
shopifytest2.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
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException, WebDriverException
from selenium.webdriver.common.by import By
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
try:
driver = webdriver.Chrome()
except WebDriverException as e:
logger.error(f"Failed to initialize Chrome driver: {e}")
exit(1)
# Open the website
try:
driver.get("https://83d8b0-75.myshopify.com/")
driver.maximize_window()
except TimeoutException as e:
logger.error(f"Failed to open website: {e}")
driver.quit()
exit(1)
# Find the search bar and enter the search query
try:
search_bar = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='search']")))
search_bar.click()
logger.info("Search bar clicked")
except TimeoutException as e:
logger.error(f"Failed to find search bar: {e}")
driver.quit()
except NoSuchElementException as e:
logger.error(f"Search bar not found: {e}")
driver.quit()
try:
search_bar = driver.find_element(By.CLASS_NAME("search__input field__input"))
search_bar.send_keys("1.5mm 5 Core Cable")
search_bar.submit(5)
except NoSuchElementException as e:
logger.error(f"Failed to find search bar: {e}")
driver.quit(5)
exit(5)
# Wait for the search results to load
try:
search_results = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, "div.search-result"))
)
except TimeoutException as e:
logger.error(f"Failed to load search results: {e}")
driver.quit(5)
exit(5)
# Click on the first search result
try:
search_results[0].click()
except IndexError as e:
logger.error(f"Failed to click on search result: {e}")
driver.quit()
exit(1)
# Wait for the product page to load
try:
product_page = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "div.product-page"))
)
except TimeoutException as e:
logger.error(f"Failed to load product page: {e}")
driver.quit()
exit(1)
# Click the "Add to cart" button
try:
add_to_cart_button = product_page.find_element(By.CSS_SELECTOR("button.add-to-cart"))
add_to_cart_button.click()
except NoSuchElementException as e:
logger.error(f"Failed to find 'Add to cart' button: {e}")
driver.quit()
exit(1)
# Wait for the popup menu to appear
try:
popup_menu = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "div.popup-menu"))
)
except TimeoutException as e:
logger.error(f"Failed to load popup menu: {e}")
driver.quit()
exit(1)
# Click the "View in cart" option
try:
view_in_cart_option = popup_menu.find_element(By.CSS_SELECTOR("a.view-in-cart"))
view_in_cart_option.click()
except NoSuchElementException as e:
logger.error(f"Failed to find 'View in cart' option: {e}")
driver.quit()
exit(1)
# Wait for the cart page to load
try:
cart_page = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "div.cart-page"))
)
except TimeoutException as e:
logger.error(f"Failed to load cart page: {e}")
driver.quit()
exit(1)
# Click the "Checkout" button
try:
checkout_button = cart_page.find_element(By.CSS_SELECTOR("button.checkout"))
checkout_button.click()
except NoSuchElementException as e:
logger.error(f"Failed to find 'Checkout' button: {e}")
driver.quit()
exit(1)
# Wait for the checkout page to load
try:
checkout_page = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "div.checkout-page"))
)
except TimeoutException as e:
logger.error(f"Failed to load checkout page: {e}")
driver.quit()
exit(1)
# Fill in the email and other fields
try:
email_field = checkout_page.find_element(By.NAME("email"))
email_field.send_keys("[email protected]")
other_fields = [
("name", "John Doe"),
("address", "123 Main St"),
("city", "Anytown"),
("state", "CA"),
("zip", "12345"),
("phone", "555-555-5555")
]
for field, value in other_fields:
field_element = checkout_page.find_element(By.NAME(field))
field_element.send_keys(value)
except NoSuchElementException as e:
logger.error(f"Failed to find field: {e}")
driver.quit()
exit(1)
# Close the browser
driver.quit()