[Python] Selenium script for Amazon UK postcode entry triggers CAPTCHA and fails to apply zip...

Discussão em 'Python' iniciado por Stack, Abril 1, 2025.

  1. Stack

    Stack Membro Participativo

    Automating Amazon UK with Selenium: Handling CAPTCHA, Setting Postcode, and Extracting Product Data


    I'm automating Amazon UK (www.amazon.co.uk) using Selenium to:

    1. Decline cookies (if present).
    2. Click the address block to open the postcode modal.
    3. Enter a postcode (e.g., "EC1A 1BB") and apply it.
    Issues:

    • Triggers a CAPTCHA after clicking the cookie decline button. The script does not proceed to change the address/postcode elements.
    • Fails to apply the postcode (no error, but the page doesn't update).
    My Script


    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
    import time

    # Create Chrome Options Object
    options = webdriver.ChromeOptions()

    # Set window size and add other configuration options
    options.add_argument("--window-size=1920,1080")
    options.add_argument("--disable-extensions")
    options.add_argument("--disable-popup-blocking")
    options.add_argument("--disk-cache-size=1")
    options.add_argument("--disable-blink-features=AutomationControlled")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])

    # Initialize Driver
    driver = webdriver.Chrome(options=options)

    try:
    driver.get("https://www.amazon.co.uk")
    wait = WebDriverWait(driver, 10)

    # Handle cookies (Decline if present)
    try:
    decline_btn = wait.until(EC.element_to_be_clickable((By.ID, "sp-cc-rejectall-link")))
    decline_btn.click()
    except Exception:
    print("Cookie decline button not found or already handled.")

    # Click address block to open the postcode modal
    address_block = wait.until(EC.element_to_be_clickable((By.ID, "GLUXAddressBlock")))
    address_block.click()

    # Enter the postcode
    postcode_field = wait.until(EC.presence_of_element_located((By.ID, "GLUXZipUpdateInput")))
    postcode_field.clear()
    postcode_field.send_keys("EC1A 1BB")

    # Apply postcode using JavaScript click
    apply_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@aria-labelledby='GLUXZipUpdate-announce']")))
    driver.execute_script("arguments[0].click();", apply_button)

    # Short wait for address update
    time.sleep(1.5)

    except Exception as e:
    print("Error:", e)
    finally:
    driver.quit()

    What I’ve Tried:

    • Added --disable-blink-features=AutomationControlled and excludeSwitches to Chrome options.
    • Used explicit waits (WebDriverWait) for elements.
    • Executed JavaScript clicks to avoid interception.
    Questions:

    1. How can I avoid CAPTCHA detection in Selenium for Amazon?
    2. How to implement product search after the postcode update?
    3. How to extract product data (e.g., XPath/CSS selectors) and save it to CSV?
    4. Search for "earphones", extract product titles, prices, and URLs into a CSV.

    Would appreciate any insights!

    Continue reading...

Compartilhe esta Página