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:
- Decline cookies (if present).
- Click the address block to open the postcode modal.
- 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
andexcludeSwitches
to Chrome options. - Used explicit waits (
WebDriverWait
) for elements. - Executed JavaScript clicks to avoid interception.
Questions:
- How can I avoid CAPTCHA detection in Selenium for Amazon?
- How to implement product search after the postcode update?
- How to extract product data (e.g., XPath/CSS selectors) and save it to CSV?
- 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:
- Decline cookies (if present).
- Click the address block to open the postcode modal.
- 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
andexcludeSwitches
to Chrome options. - Used explicit waits (
WebDriverWait
) for elements. - Executed JavaScript clicks to avoid interception.
Questions:
- How can I avoid CAPTCHA detection in Selenium for Amazon?
- How to implement product search after the postcode update?
- How to extract product data (e.g., XPath/CSS selectors) and save it to CSV?
- 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 badges1 Answer
Reset to default 0I 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()