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

Selenium运行OpenSea NFT批量上传脚本报TimeoutException排查方案

问题概述
  • 场景:使用基于Selenium开发的Python脚本,实现OpenSea平台NFT批量上传全流程自动化操作
  • 现象:脚本每次运行都会抛出超时异常,自行查阅资料调整等待时长参数后,问题仍未解决
  • 基础:具备一定Python开发基础,无Selenium使用经验
  • 补充:初步怀疑异常与Chrome浏览器启动配置段有关
报错日志
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Loula\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "C:\Users\Loula\Downloads\NFTs-Upload-to-OpenSea-main\openseaupload.py", line 137, in main_program_loop
    wait_xpath('//*[@id="__next"]/div[1]/main/div/div/div[1]/span/a')
  File "C:\Users\Loula\Downloads\NFTs-Upload-to-OpenSea-main\openseaupload.py", line 129, in wait_xpath
    wait.until(ExpectedConditions.presence_of_element_located((By.XPATH, code)))
  File "C:\Users\Loula\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
关联核心代码片段
opt = Options()
opt.add_experimental_option("debuggerAddress", "localhost:8989")
driver = webdriver.Chrome(
    executable_path=project_path + "/chromedriver.exe",
    chrome_options=opt,
)
wait = WebDriverWait(driver, 60)
###wait for methods
def wait_css_selector(code):
    wait.until(
        ExpectedConditions.presence_of_element_located((By.CSS_SELECTOR, code))
    )
    
def wait_css_selectorTest(code):
    wait.until(
        ExpectedConditions.element_to_be_clickable((By.CSS_SELECTOR, code))
    )    
def wait_xpath(code):
    wait.until(ExpectedConditions.presence_of_element_located((By.XPATH, code)))
while end_num >= start_num:
    print("Start creating NFT " +  loop_title + str(start_num))
    driver.get(collection_link)
    # time.sleep(3)
    wait_xpath('//*[@id="__next"]/div[1]/main/div/div/div[1]/span/a')
    additem = driver.find_element_by_xpath('//*[@id="__next"]/div[1]/main/div/div/div[1]/span/a')
    additem.click()
    time.sleep(1)
    wait_xpath('//*[@id="media"]')
    imageUpload = driver.find_element_by_xpath('//*[@id="media"]')
    imagePath = os.path.abspath(file_path + "\\" + str(start_num) + "." + loop_file_format)  # change folder here
    imageUpload.send_keys(imagePath)
    name = driver.find_element_by_xpath('//*[@id="name"]')
    name.send_keys(loop_title + str(start_num))  # +1000 for other folders #change name before "#"
    time.sleep(0.5)
    ext_link = driver.find_element_by_xpath('//*[@id="external_link"]')
    ext_link.send_keys(loop_external_link)
    time.sleep(0.5)
    desc = driver.find_element_by_xpath('//*[@id="description"]')
    desc.send_keys(loop_description)
    time.sleep(0.5)
可行排查方向
  • 校验Chrome调试端口启动逻辑:代码通过debuggerAddress配置连接本地8989端口的已启动Chrome实例,必须提前手动以调试模式启动Chrome,否则Selenium连接的浏览器上下文异常,无法加载目标页面元素。手动启动命令为chrome.exe --remote-debugging-port=8989 --user-data-dir="C:\chrome_debug_profile",启动后提前完成OpenSea账号登录,再运行脚本,不要依赖脚本自动启动浏览器。
  • 核对Chrome与chromedriver版本匹配性:脚本调用本地路径下的chromedriver.exe,必须与当前运行的Chrome大版本完全一致,版本不匹配会引发页面渲染、元素定位异常,即使成功连接浏览器也会出现必现/偶发的元素查找失败问题。
  • 替换硬编码绝对路径XPath:脚本使用的//*[@id="__next"]/div[1]/main/div/div/div[1]/span/a是开发者工具生成的绝对层级路径,OpenSea前端迭代频繁,DOM层级、节点挂载位置经常调整,这类硬编码路径只要页面结构发生微小变动就会直接失效。可手动打开目标收藏集页面,基于元素特征替换为更稳定的定位规则,比如通过按钮文本匹配的相对XPath//a[contains(text(),'Add item')],避免使用全层级路径定位。
  • 确认页面跳转后的实际加载状态:执行driver.get(collection_link)跳转后,如果触发未登录重定向、Cloudflare人机验证、地区访问限制,页面不会加载到收藏集主页,自然找不到添加NFT的入口按钮。可在跳转代码后增加time.sleep(5)的临时等待,直接观察绑定的浏览器窗口实际加载的页面内容,不要默认页面已正常加载到目标地址。
  • 修正Selenium版本兼容参数:Selenium 4.0及以上版本初始化Chrome驱动时,已废弃chrome_options入参,需替换为options=opt,旧参数会导致配置加载异常,驱动无法正确绑定到调试端口的Chrome实例。
  • 调整元素等待的判断规则:当前使用的presence_of_element_located仅判断元素存在于DOM树中,不校验元素是否可见、可交互,前端渲染时元素可能已存在于DOM但被遮罩层遮挡、未完成可视区渲染,会导致后续点击操作失败。针对按钮类可交互元素,替换为element_to_be_clickable判断条件,可靠性更高。

内容的提问来源于stack exchange,提问作者L.Bar5522

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 01:25:05