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

Selenium未调用driver.quit()意外关闭窗口报错(PyCharm环境)

Selenium浏览器意外关闭+NoSuchElementException问题解决

环境与问题

使用最新版Selenium、PyCharm开发,Chrome版本115.0.5790.171。执行代码时浏览器打开后立刻关闭,同时抛出NoSuchElementException,提示无法定位#articalcount a元素。

涉事代码

from selenium import webdriver
from selenium.webdriver.common.by import By


driver = webdriver.Chrome()
driver.get(url="https://en.wikipedia.org/wiki/Main_Page")
article_count = driver.find_element(By.CSS_SELECTOR, "#articalcount a")
article_count.click()

报错信息

初始栈追踪

Stacktrace:
Backtrace:
    GetHandleVerifier [0x00007FF62C894A62+57106]
    (No symbol) [0x00007FF62C80CF52]
    (No symbol) [0x00007FF62C6DE2CB]
    (No symbol) [0x00007FF62C71786E]
    (No symbol) [0x00007FF62C71795C]
    (No symbol) [0x00007FF62C750477]
    (No symbol) [0x00007FF62C7369FF]
    (No symbol) [0x00007FF62C74E522]
    (No symbol) [0x00007FF62C736793]
    (No symbol) [0x00007FF62C70CE81]
    (No symbol) [0x00007FF62C70E064]
    GetHandleVerifier [0x00007FF62CB44222+2873042]
    GetHandleVerifier [0x00007FF62CB96590+3209792]
    GetHandleVerifier [0x00007FF62CB8F3AF+3180639]
    GetHandleVerifier [0x00007FF62C925F25+652245]
    (No symbol) [0x00007FF62C818618]
    (No symbol) [0x00007FF62C8147C4]
    (No symbol) [0x00007FF62C8148BC]
    (No symbol) [0x00007FF62C804C33]
    BaseThreadInitThunk [0x00007FFF274026AD+29]
    RtlUserThreadStart [0x00007FFF2894AA68+40]

完整错误栈

Traceback (most recent call last):
  File "C:\Users\lyuan\Desktop\100_days_of_code\day48\main.py", line 7, in <module>
    article_count = driver.find_element(By.CSS_SELECTOR, "#articalcount a")
  File "C:\Users\lyuan\Desktop\100_days_of_code\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 739, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
  File "C:\Users\lyuan\Desktop\100_days_of_code\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 345, in execute
    self.error_handler.check_response(response)
  File "C:\Users\lyuan\Desktop\100_days_of_code\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#articalcount a"}
  (Session info: chrome=115.0.5790.171); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
Backtrace:
    GetHandleVerifier [0x00007FF7BB4B4A62+57106]
    (No symbol) [0x00007FF7BB42CF52]
    (No symbol) [0x00007FF7BB2FE2CB]
    (No symbol) [0x00007FF7BB33786E]
    (No symbol) [0x00007FF7BB33795C]
    (No symbol) [0x00007FF7BB370477]
    (No symbol) [0x00007FF7BB3569FF]
    (No symbol) [0x00007FF7BB36E522]
    (No symbol) [0x00007FF7BB356793]
    (No symbol) [0x00007FF7BB32CE81]
    (No symbol) [0x00007FF7BB32E064]
    GetHandleVerifier [0x00007FF7BB764222+2873042]
    GetHandleVerifier [0x00007FF7BB7B6590+3209792]
    GetHandleVerifier [0x00007FF7BB7AF3AF+3180639]
    GetHandleVerifier [0x00007FF7BB545F25+652245]
    (No symbol) [0x00007FF7BB438618]
    (No symbol) [0x00007FF7BB4347C4]
    (No symbol) [0x00007FF7BB4348BC]
    (No symbol) [0x00007FF7BB424C33]
    BaseThreadInitThunk [0x00007FFD436A26AD+29]
    RtlUserThreadStart [0x00007FFD4484AA68+40]

解决步骤

  1. 修复元素定位拼写错误
    核心问题是CSS选择器拼写错误:#articalcount应为#articlecount(少写了一个i)。维基百科主页上统计文章数量的元素ID是articlecount,修正后即可定位到元素。

  2. 防止浏览器自动关闭
    代码执行完毕后Python进程结束,Selenium会默认关闭浏览器。可以通过以下方式保持浏览器打开:

    • 在代码末尾添加input("Press Enter to close..."),让程序等待用户输入后再结束;
    • 使用with语句管理driver会话,确保会话在需要时保持:
      from selenium import webdriver
      from selenium.webdriver.common.by import By
      
      with webdriver.Chrome() as driver:
          driver.get(url="https://en.wikipedia.org/wiki/Main_Page")
          article_count = driver.find_element(By.CSS_SELECTOR, "#articlecount a")
          article_count.click()
          input("Press Enter to close...")
      

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 06:16:03