Browse Source

Changed script2 to full blown headless browser transaction, similar to 1

main
kusum 6 months ago
parent
commit
d2a00daf28
1 changed files with 67 additions and 36 deletions
  1. 67
    36
      GSAutomation/scripts/cpr_trafficscript2.py

+ 67
- 36
GSAutomation/scripts/cpr_trafficscript2.py View File

#!/usr/bin/env python3 #!/usr/bin/env python3


import time
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager from webdriver_manager.chrome import ChromeDriverManager
import time

# Function to prompt user for user ID
def get_user_id():
return input("Please enter your user ID: ")

# Set up Chrome options
chrome_options = Options()
chrome_options.add_argument("--headless") # Run headless Chrome, comment out if you want to see the browser

# Use WebDriverManager to automatically use the system's ChromeDriver
service = ChromeService(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)


# Prompt user for user ID
user_id = get_user_id()

# List of paths to test
paths = ['/', '/shop/', '/product/blue-sweater/', '/product/custom-tee/', '/product-category/clothing/dresses/']

# Base URL
base_url = f'http://{user_id}.cpr-akashop.com'

# Function to measure load time
def measure_load_time(url):
start_time = time.time()
def search_for_dress(driver, user_id):
url = f'https://{user_id}.cpr-akashop.com/'
driver.get(url) driver.get(url)
# Wait for the page to completely load (adjust as necessary)
time.sleep(5) # Simple wait; use WebDriverWait for more precise control
end_time = time.time()
return end_time - start_time
print(f"Navigated to {url}")
time.sleep(1)
# Find and click on the magnifying glass icon
magnifying_glass = driver.find_element(By.CSS_SELECTOR, '#header-grid > nav.header--row.header-main.hide-on-mobile.hide-on-tablet.layout-full-contained.nv-navbar.header--row > div > div > div > div.hfg-slot.right > div > div.item--inner.builder-item--header_search_responsive > div > div > a > svg')
magnifying_glass.click()
print("Clicked on magnifying glass icon")
time.sleep(1)
# Find the search box and search for "dress"
search_box = driver.find_element(By.CSS_SELECTOR, '#header-grid > nav.header--row.header-main.hide-on-mobile.hide-on-tablet.layout-full-contained.nv-navbar.header--row > div > div > div > div.hfg-slot.right > div > div.item--inner.builder-item--header_search_responsive > div > div > div > div.form-wrap > form > input')
search_box.send_keys('dress')
search_box.submit()
print("Searched for 'dress'")
time.sleep(3)
# Find and click on the Striped Dress search result
striped_dress_link = driver.find_element(By.CSS_SELECTOR, '#post-82 > div > div > div > div.non-grid-content.alternative-layout-content > h2 > a')
# Scroll to the element
driver.execute_script("arguments[0].scrollIntoView(true);", striped_dress_link)
time.sleep(1) # Add a small delay after scrolling
striped_dress_link.click()
print("Clicked on the Striped Dress link")
time.sleep(3)
# Add the Striped Dress to the cart
add_to_cart_button = driver.find_element(By.CSS_SELECTOR, '#product-82 > div.nv-single-product-top > div.summary.entry-summary > form > button')
add_to_cart_button.click()
print("Added Striped Dress to the cart")
time.sleep(3)
# Click on View Cart
view_cart_button = driver.find_element(By.CSS_SELECTOR, '#content > div > div > div > div.woocommerce-notices-wrapper > div > a')
view_cart_button.click()
print("Clicked on View Cart")
time.sleep(3)
# Scroll to the Proceed to Checkout button
proceed_to_checkout_button = driver.find_element(By.CSS_SELECTOR, '#content > div > div > div > div.nv-content-wrap.entry-content > div > div.cart-collaterals > div > div > a')
driver.execute_script("arguments[0].scrollIntoView(true);", proceed_to_checkout_button)
time.sleep(1) # Add a small delay after scrolling
# Click on Proceed to Checkout
proceed_to_checkout_button.click()
print("Clicked on Proceed to Checkout")
time.sleep(3)

def main(user_id):
# Set up Chrome options
chrome_options = Options()
chrome_options.add_argument("--headless") # Run headless Chrome
chrome_options.add_argument("--disable-gpu") # Disable GPU acceleration
chrome_options.add_argument("--window-size=1920,1200") # Set window size to avoid issues with elements being out of view


# Test each path
for path in paths:
full_url = base_url + path
load_time = measure_load_time(full_url)
print(f"Load time for {full_url}: {load_time:.2f} seconds")
# Use WebDriverManager to automatically manage the ChromeDriver installation
service = ChromeService(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)
search_for_dress(driver, user_id)
driver.quit()
print("Browser closed")


# Close the driver
driver.quit()
if __name__ == "__main__":
user_id = input("Please enter your user ID: ") # Ask for the user ID only once
main(user_id)

Loading…
Cancel
Save