0% found this document useful (0 votes)
6 views1 page

Automate Amazon Cart with Selenium

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Automate Amazon Cart with Selenium

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

from selenium import webdriver

from [Link] import By


from [Link] import Keys
from [Link] import WebDriverWait
from [Link] import expected_conditions as EC
from [Link] import Service
from webdriver_manager.chrome import ChromeDriverManager
import time

# Setup Chrome Driver (NO PATH NEEDED)


service = Service(ChromeDriverManager().install())
options = [Link]()
options.add_argument("--start-maximized")

driver = [Link](service=service, options=options)

try:
# Open Amazon
[Link]("[Link]

# Search Box
search_box = WebDriverWait(driver, 15).until(
EC.presence_of_element_located(([Link], "twotabsearchtextbox"))
)
search_box.send_keys("laptop")
search_box.send_keys([Link])

# Wait for product results


product = WebDriverWait(driver, 15).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "h2 a"))
)
[Link]()

# Switch to new tab


driver.switch_to.window(driver.window_handles[-1])

# Add to cart
add_cart = WebDriverWait(driver, 15).until(
EC.presence_of_element_located(([Link], "add-to-cart-button"))
)
add_cart.click()

[Link](3)

# Open cart
[Link]("[Link]

# Verify cart item


cart_item = WebDriverWait(driver, 15).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "span.a-truncate-cut"))
)

print("Test Passed: Item added to cart:", cart_item.text)

except Exception as e:
print("Test Failed:", e)

finally:
[Link](5)
[Link]()

You might also like