【问题标题】:Selenium module `NoSuchElementElement` exception [PYTHON]硒模块`NoSuchElementElement`异常[PYTHON]
【发布时间】:2021-09-30 03:50:32
【问题描述】:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: #loginUsername

我使用 selenium 模块来自动化一些网站的登录过程,当我执行程序时,它抛出 NoSuchElementException 而我尝试了所有 find_element_by_* 方法(按 id、按类、按 css 选择器、按文本,按名称 attr )但是,我仍然收到此错误,并且我不知道如何使该程序无错误,那么,还有其他方法可以调试程序吗??

【问题讨论】:

  • 分享网址。如果不可能,请使用 HTML 更新问题。
  • 听起来像是在 iframe 中,请检查 iframe。
  • 没有 iframe 或者它只是被包裹在一个表单标签和一个字段集标签内

标签: python selenium exception


【解决方案1】:

我没有足够的声誉来发表评论,所以发布一个答案。我认为以下任何一个原因都可能是原因。

  • 请检查 chrome 控制台中的 xPath id 等元素,以确保您使用正确的locator

  • 没有足够的时间让元素可以访问。请使用explicit等等。

  • 当您尝试执行操作时,可能元素是 DOM 上的 present 而不是 visible

  • 从错误消息中我可以看到您正在使用css 选择器#loginUsername。我假设您没有给出 tag 的名字,就像它假设的那样是 input#loginUsername

【讨论】:

    【解决方案2】:

    我了解到您上面所说的没有 iframe。

    确保在调用 find_element(...) 之前等待元素加载并可用。

    做这样的事情:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Chrome("D:/chromedriver/94/chromedriver.exe")
    driver.get("https://yourwebsite.com")
    
    # use By.XPATH, By.CLASS_NAME etc. depending on the 
    # element you're referring to
    WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.ID, 'loginUsername')))
    login = driver.find_element(By.ID, 'loginUsername')
    

    【讨论】:

      猜你喜欢
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多