最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Selenium script for Amazon UK postcode entry triggers CAPTCHA and fails to apply zip code - Stack Overflow

programmeradmin3浏览0评论

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.webdrivermon.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(";)
    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!

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.webdrivermon.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!

Share Improve this question asked Apr 1 at 12:30 Luis swiftLuis swift 631 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I did not get CAPTCHA when running the selenium code. It randomly pops-up perhaps. Anyway this answer is not how to handle captcha.

Apart from captcha, there are other issues in your code. Example the locators, none of the locators seems to be correct. I couldn't locate anything with this ID - GLUXAddressBlock

Refer the code below where I have fixed all the locators and removed unnecessary lines of code:

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

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 on Update location
wait.until(EC.element_to_be_clickable((By.ID, "glow-ingress-line2"))).click()

# Enter text in postcode text box
wait.until(EC.element_to_be_clickable((By.ID, "GLUXZipUpdateInput"))).send_keys("EC1A 1BB")

# Click on Apply button
wait.until(EC.element_to_be_clickable((By.ID, "GLUXZipUpdate"))).click()

time.sleep(10)
driver.quit()

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论