You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python Selenium修改Amazon.co.uk的配送国家并截图?

解决Amazon.co.uk配送地址修改问题

问题描述

我需使用Python的Selenium包在Amazon.co.uk上搜索关键词并截取网页截图,但当前页面显示的配送地址为美国。请问如何通过编程方式修改页面上的"Deliver to Country"设置?

解决方案

要修改配送地址,需模拟用户操作流程进入地址设置面板,选择目标国家(此处以英国为例),具体步骤:

  • 先处理Cookie弹窗,避免后续操作受阻
  • 定位页面顶部的"Deliver to"区域并点击,打开地址设置面板
  • 点击面板中的"Change"按钮进入国家选择页面
  • 选择目标国家并保存设置
  • 完成设置后再执行搜索和截图操作

修改后的完整代码

import time as t
from datetime import datetime
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--headless=new")  # 新版无头模式更稳定
chrome_options.add_argument('window-size=2160x3840')

urls = ['https://www.amazon.co.uk/s?k=advil', 'https://www.amazon.co.uk/s?k=Whitening toothpaste']

def get_screenshots(url):
    driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
    wait = WebDriverWait(driver, 10)
    
    try:
        driver.get('https://www.amazon.co.uk')
        
        # 拒绝所有Cookie
        try:
            reject_btn = wait.until(EC.element_to_be_clickable((By.ID, "sp-cc-rejectall-link")))
            reject_btn.click()
            print("已拒绝Cookie")
        except:
            pass
        
        # 修改配送地址为英国
        # 点击顶部Deliver to区域
        deliver_to = wait.until(EC.element_to_be_clickable((By.ID, "glow-ingress-block")))
        deliver_to.click()
        
        # 点击Change按钮
        change_btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Change']")))
        change_btn.click()
        
        # 选择英国
        country_input = wait.until(EC.visibility_of_element_located((By.ID, "GLUXCountryListDropdown")))
        country_input.click()
        uk_option = wait.until(EC.element_to_be_clickable((By.XPATH, "//a[text()='United Kingdom']")))
        uk_option.click()
        
        # 保存设置
        save_btn = wait.until(EC.element_to_be_clickable((By.ID, "GLUXConfirmButton")))
        save_btn.click()
        
        # 进入搜索页面并调整缩放
        t.sleep(2)
        driver.get(url)
        driver.execute_script("document.body.style.zoom='50%'")
        
        # 等待搜索结果加载完成
        wait.until(EC.presence_of_element_located((By.ID, "search")))
        
        # 生成截图并保存
        now = datetime.now()
        date_time = now.strftime("%Y_%m_%d_%H_%M_%S")
        sh_url = url.split('?k=')[1].replace(' ', '_')
        screenshot_path = f'{sh_url}_{date_time}.png'
        driver.save_screenshot(screenshot_path)
        print(f"已保存截图: {screenshot_path}")
        
    except Exception as e:
        print(f"操作出错: {e}")
    finally:
        t.sleep(2)
        driver.quit()

for url in urls:    
    get_screenshots(url)

代码说明

  • 用WebDriverWait替代硬等待,提升代码稳定性,避免元素未加载导致的错误
  • 适配新版Chrome的无头模式,替换为--headless=new参数
  • 完善用户代理配置,降低被网站识别为爬虫的概率
  • 增加错误捕获逻辑,确保程序异常时能正常关闭浏览器
  • 处理搜索关键词中的空格,避免截图文件名出现格式问题

内容的提问来源于stack exchange,提问作者Fisseha Berhane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 23:31:24