-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_workflow.py
More file actions
75 lines (61 loc) · 2.75 KB
/
Copy pathtest_workflow.py
File metadata and controls
75 lines (61 loc) · 2.75 KB
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
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import time
# 1. Setup Chrome Browser
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.maximize_window()
wait = WebDriverWait(driver, 15) # Increased wait to 15s for stability
try:
# 2. Go to the website
driver.get("https://automationexercise.com")
print("Navigated to website.")
# 3. Handle Ads (6-second initial wait)
print("Waiting 6 seconds for potential ads...")
time.sleep(6)
# Remove any ad overlays using JavaScript
driver.execute_script("""
var ads = document.querySelectorAll('ins, iframe, .adsbygoogle');
for (var i = 0; i < ads.length; i++) {
ads[i].remove();
}
""")
print("Cleaned up ad overlays.")
# 4. In navbar click Home
home_button = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Home")))
home_button.click()
print("Clicked Home.")
# 5. Left side Brand container click 'Polo'
# Use JS click to avoid 'ElementClickIntercepted'
polo_brand = wait.until(EC.presence_of_element_located((By.XPATH, "//a[@href='/brand_products/Polo']")))
driver.execute_script("arguments[0].scrollIntoView(true);", polo_brand)
driver.execute_script("arguments[0].click();", polo_brand)
print("Clicked Polo Brand.")
# 6. Find 'Premium Polo T-Shirts' and click 'Add to cart'
# We target the 'Add to cart' button specifically for product 2 (Polo T-Shirt)
add_to_cart = wait.until(EC.presence_of_element_located((By.XPATH, "//div[@class='productinfo text-center']//a[@data-product-id='2']")))
driver.execute_script("arguments[0].scrollIntoView(true);", add_to_cart)
time.sleep(1) # Small pause for scroll to finish
driver.execute_script("arguments[0].click();", add_to_cart)
print("Added Premium Polo T-Shirt to cart.")
# 7. Click 'View Cart' link in the popup
view_cart_link = wait.until(EC.element_to_be_clickable((By.XPATH, "//u[text()='View Cart']")))
view_cart_link.click()
print("Clicked View Cart.")
# 8. Wait for 10 seconds on the cart page
print("Waiting for 10 seconds on the cart page...")
time.sleep(10)
# 9. Click 'Proceed To Checkout'
checkout_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".check_out")))
checkout_button.click()
print("Clicked Proceed To Checkout.")
print("Workflow completed successfully!")
except Exception as e:
print(f"An error occurred: {e}")
finally:
time.sleep(5)
driver.quit()