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

Selenium Python中.click()在子类别元素上失效的问题求助

问题描述

我正在开发一个自动填写表单完成发布准备的简单项目,前面的步骤都能正常运行,但处理子类别元素时提示无法找到该元素。页面有主类别和子类别两个相似元素,我是Selenium新手,试过类名等不同定位方式都没解决,求解决方案或建议。

以下是我的代码:

from msilib.schema import Condition
from tokenize import Name
from turtle import color
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome("D:/chromedriver.exe")
driver.get("https://www.grailed.com/sell")
time.sleep(2)
driver.find_element('xpath', '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/p[2]/a').click()
time.sleep(2)
driver.find_element('xpath', '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/button[4]').click()
google_email = driver.find_element('xpath', '//*[@id="email"]')
google_email.send_keys("@gmail.com")
driver.find_element('xpath', '//*[@id="password"]').send_keys('')
time.sleep(1)
driver.find_element('xpath', '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/form/button').click()
time.sleep(3)
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/input').click()
time.sleep(2)
#Start of entering clothing details
#picking type of clothing, men or woman
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/div/span[1]').click()
time.sleep(2)
#selects the main category
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/div/span[2]').click()
time.sleep(2)
something = 'something'
driver.find_element('xpath', '//*[@id="designer-autocomplete"]').send_keys(something)
time.sleep(1)


#picking the sub-category, but this not working for some reason
#Error states unable to locate element
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[2]/div/input').click()
time.sleep(2)
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[2]/div/div/div/span[4]').click()
time.sleep(2)
解决方案
  • 替换固定sleep为显式等待:time.sleep()是硬等待,页面加载延迟会导致元素还未渲染就执行定位操作,改用WebDriverWait能在元素满足可点击/可见条件后再执行操作,稳定性大幅提升。
  • 修正子类别元素的定位路径:你的子类别选项xpath写错了,主类别选择后,子类别选项的层级应该和主类别一致(div[1]/div/div[1]/div[2]而非div[1]/div/div[2]),需要用浏览器开发者工具重新确认元素的真实路径。
  • 移除无用导入:代码里msilib.schema.Condition、tokenize.Name、turtle.color这些导入完全没用,建议删除,避免冗余。
  • 优先使用稳定定位属性:能用id定位的元素(比如邮箱输入框email、密码框password)就不要用冗长的xpath,减少页面结构变化带来的定位失效问题。

修改后的代码示例

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

driver = webdriver.Chrome("D:/chromedriver.exe")
# 设置10秒的显式等待超时时间
wait = WebDriverWait(driver, 10)

driver.get("https://www.grailed.com/sell")

# 点击登录相关按钮
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/p[2]/a'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/button[4]'))).click()

# 输入登录信息
email_input = wait.until(EC.visibility_of_element_located((By.ID, "email")))
email_input.send_keys("your_email@gmail.com")
password_input = driver.find_element(By.ID, "password")
password_input.send_keys("your_password")

wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/form/button'))).click()

# 选择服装类型与主类别
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/input'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/div/span[1]'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/div/span[2]'))).click()

# 输入设计师名称
designer_input = wait.until(EC.visibility_of_element_located((By.ID, "designer-autocomplete")))
designer_input.send_keys("something")

# 处理子类别
# 等待子类别输入框可点击
sub_category_input = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[2]/div/input')))
sub_category_input.click()
# 等待子类别选项加载完成并点击
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[2]/div/div/span[4]'))).click()

# 操作完成后关闭浏览器
driver.quit()

内容的提问来源于stack exchange,提问作者Argel Arroyo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 09:50:24