运行Selenium时出现NoSuchElementException及相关警告的原因咨询
运行Selenium时的警告与错误原因解析
问题背景
运行Selenium脚本时出现了SLF4J警告、CDP版本匹配警告,以及核心的NoSuchElementException错误,具体日志如下:
SLF4J: No SLF4J providers were found. SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details. Starting ChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052}) on port 20315 Only local connections are allowed. Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe. ChromeDriver was started successfully. Jun 08, 2023 12:07:16 AM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch WARNING: Unable to find an exact match for CDP version 114, so returning the closest version found: 112 Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#submitBitton"} (Session info: chrome=114.0.5735.91) For documentation on this error, please visit: https://selenium.dev/exceptions/#no_such_element Build info: version: '4.9.0', revision: 'd7057100a6' System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '20.0.1' Driver info: org.openqa.selenium.chrome.ChromeDriver Command: [5c61470e07d5dbe7098dbbc61ef088a9, findElement {using=id, value=submitBitton}] Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 114.0.5735.91, chrome: {chromedriverVersion: 114.0.5735.90 (386bc09e8f4f..., userDataDir: C:\Users\Dell\AppData\Local...}, goog:chromeOptions: {debuggerAddress: localhost:61375}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:61375/devtoo..., se:cdpVersion: 114.0.5735.91, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true} Session ID: 5c61470e07d5dbe7098dbbc61ef088a9 at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:67) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:484) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:193) at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:183) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:158) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543) at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:162) at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:60) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344) at login.runTestCases.main(runTestCases.java:16)
各警告/错误的原因
1. SLF4J相关警告
- 项目只引入了SLF4J的API依赖,但没添加具体的日志实现库(比如Logback、Log4j2等)。SLF4J本身只是日志门面,需要搭配具体实现类才能输出日志,找不到实现时就会默认用无操作的NOP实现,不会影响脚本执行,但日志信息无法正常打印。
2. CDP版本不匹配警告
- 你用的Chrome浏览器版本是114,但当前Selenium版本(4.9.0)内置的Chrome DevTools Protocol(CDP)仅支持到112版本。Selenium会自动匹配最接近的CDP版本来兼容,基础页面操作一般不受影响,但如果用到Chrome 114新增的CDP特性,可能会出现功能异常。
3. NoSuchElementException错误
- 最直接的原因是元素ID拼写错误:代码中查找的元素ID是
submitBitton,但页面上实际按钮的ID应该是submitButton(把Button误写成了Bitton)。 - 除此之外还有几种常见可能(不过本次日志里明显是拼写问题):
- 元素还未加载完成就执行了查找操作,需要添加显式等待确保元素出现;
- 目标元素嵌套在
<iframe>或<frame>标签内,需要先切换到对应框架才能查找; - 元素是动态生成的,页面加载后才会渲染出来,需要等待动态内容加载完成。
内容的提问来源于stack exchange,提问作者H L
相关产品推荐
相关产品推荐

