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

使用Selenium编写代码时遇NameError错误,请求技术协助

解决Selenium中NameError: name 'find_element' is not defined错误
  • 错误根源:你直接全局调用find_element函数,但Selenium的find_element必须通过WebDriver实例对象调用,无法直接使用。

  • 修复方案:

    1. 先确保已正确初始化WebDriver实例(以Chrome为例):
      from selenium import webdriver
      from selenium.webdriver.common.by import By
      from time import sleep
      
      # 初始化Chrome驱动
      driver = webdriver.Chrome()
      
    2. 修改你的代码,通过driver实例调用find_element:
      button_login = driver.find_element(By.CSS_SELECTOR, '#loginForm > div > div:nth-child(3) > button')
      button_login.click()
      sleep(2)
      
  • 注意事项:如果使用Selenium 4.x版本,上述写法完全兼容,同时要保证By类已正确导入,且WebDriver实例处于正常运行状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 03:55:17