1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| # 1.目标网址:https://www.taobao.com/ # 2.要求:使用selenium技术在目标网站中进行自动化订单 # 3.步骤: # (1)登陆目标网址,点击“立即登录”; # (2)设置动态等待60秒,使用扫码登陆,并点击保持; # (3)在主页搜索栏左端中选择“店铺”,并在搜索框中输入“华为官方旗舰店”,点击“搜索”; # (4)在搜索结果中选择第一条“华为官方旗舰店”,点击进入店铺主页; # (5)点击“全部商品分类”,点击第一个商品“华为/HUAWEI Mate 60 Pro”; # (6)选择参数“雅丹黑”和“12GB+256GB”,点击领券购买
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time
# 配置浏览器驱动 options = webdriver.ChromeOptions() options.add_argument("--start-maximized") # 启动时窗口最大化 driver = webdriver.Chrome(options=options)
try: # 1. 打开目标网址 driver.get("https://www.taobao.com/")
# 2. 点击“立即登录” login_button = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.LINK_TEXT, "亲,请登录")) ) login_button.click()
# 3. 设置动态等待并扫码登录 WebDriverWait(driver, 60).until( EC.presence_of_element_located((By.CLASS_NAME, "site-nav-login-info-nick")) ) print("登录成功")
# 处理扫码登录后的弹窗 try: # 等待弹窗的出现,并点击确认或保持 keep_button = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.LINK_TEXT, "保持")) ) keep_button.click() print("已保持设置") except Exception as e: print("未出现保持弹窗或操作超时,继续执行后续操作")
# 处理“是否订阅店铺消息”的弹窗 try: cancel_button = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'取消')]")) ) cancel_button.click() print("已关闭订阅店铺消息弹窗") except Exception as e: print("未出现订阅弹窗或操作超时,继续执行后续操作")
# 4. 鼠标悬停在搜索栏左侧的“宝贝”两个字上 try: baby_label = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.XPATH, '//*[@id="J_Search"]/div[1]/ul')) ) # 创建动作链并悬停 ActionChains(driver).move_to_element(baby_label).perform() print("已悬停在'宝贝'上") # 等待下拉列表出现并点击“店铺” shop_option = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, '//*[@id="J_Search"]/div[1]/ul/li[3]')) ) shop_option.click() print("已选择店铺")
except Exception as e: print("未能找到'宝贝'标签或操作超时。", e)
# 继续搜索操作 search_box = driver.find_element(By.ID, "q") search_box.send_keys("华为官方旗舰店")
search_button = driver.find_element(By.CLASS_NAME, "btn-search") search_button.click()
# 5. 点击第一条搜索结果进入店铺 first_shop = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, '//*[@id="srp_shopLayout_shopContentWrapper"]/a[1]/div[2]/div[1]/div[2]/div[4]/div/div')) ) first_shop.click()
# 切换到新打开的窗口 driver.switch_to.window(driver.window_handles[-1])
# 6. 点击“全部商品分类” all_products = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, '//*[@id="shop23245066575"]/div/div[2]/div/div/a[1]')) ) all_products.click()
# # 等待页面加载 # WebDriverWait(driver, 10).until( # EC.presence_of_element_located((By.XPATH, '//*[@id="J_ShopSearchResult"]/div/div[3]/div[1]/dl[1]/dt/a/img')) # )
# 切换到新打开的窗口 driver.switch_to.window(driver.window_handles[-1]) # 点击第一个商品“华为/HUAWEI Mate 60 Pro” first_product = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div[3]/div[2]/div/div[1]/div/div/div/div/div/div[3]/div[1]/dl[1]/dt/a/img')) ) first_product.click()
# 切换到新打开的窗口 # 选择参数“雅丹黑”和“12GB+256GB” # 选择参数并下单 driver.switch_to.window(driver.window_handles[-1])
color_option = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div/div[2]/div[1]/div[2]/div/div[1]/div[3]/div[2]/div[8]/div/div/div[1]/div[1]/div[2]/div[1]/img')) ) color_option.click() print("已选择参数") time.sleep(1)
#选择内存
# # 滚动到颜色选项并点击 # driver.execute_script("arguments[0].scrollIntoView(true);", color_option) # color_option.click()
# 等待规格选项可点击 spec_option = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, '/html/body/div[3]/div/div[2]/div[1]/div[2]/div/div[1]/div[3]/div[2]/div[8]/div/div/div[1]/div[2]/div[2]/div[1]/span')) # 替换为实际的 XPath ) driver.execute_script("arguments[0].scrollIntoView(true);", spec_option) spec_option.click() print("内存")
# 点击领券购买 purchase_button = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div/div[2]/div[1]/div[2]/div/div[1]/div[3]/div[3]/div/div[1]/button[1]/span')) ) purchase_button.click()
print("自动下单流程完成") except Exception as e: print(f"发生错误:{e}") finally: time.sleep(5) # 保持窗口5秒方便查看 driver.quit()
|