0

Why these appear when I run my selenium:

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)

Why the error happen?

Shawn
  • 4,064
  • 2
  • 11
  • 23
H L
  • 1

2 Answers2

0

I notice this in the error:

#submitBitton

I think it should be:

#submitButton

If that was a typo, correct it in your findElement code.

About the error you are getting, selenium is not able to locate the element using the locator in your code.

Refer this link: NoSuchElementException

Shawn
  • 4,064
  • 2
  • 11
  • 23
0

As the name suggests, a NoSuchElementException means that the web driver was not able to find the element on the page. This occurs under two circumstances:

  1. Erroneous identification of element - It was suggested in this answer that the submitted ID was typed incorrectly ('submitBitton' instead of 'submitButton'). So, obviously, if the wrong identification element is provided to the web driver, it is not going to be able to find it let alone interact with it.
  2. Not enough time was given to the page to fully load - This is the most common reason why inexperienced developers run into this sort of issue. They expect for all pages to load instantly. Even in cases when this is the expected behavior, I find it to be prudent to instruct the web driver to "wait a little" before attempting to locate a component, or in some cases to "wait until" a certain condition is met. These two wait mechanisms are known as "Implicit Wait" and "Explicit Wait"

Implicit Wait

Implicit wait directs the web driver to wait for a specific amount of time before attempting to execute a test. Once this wait period concludes, the web driver will be unblocked and will proceed with the test execution.

An example using Chrome Driver:

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

This is typically done as a setup for all your tests. Once you set an implicit wait, it is set for the life of the test session you are running.


Explicit Wait

This is the preferred wait, in my opinion. Implicit wait pauses the web driver. Explicit wait tells the driver to wait a maximum amount of time OR until a condition is met. For this, the web driver polls in intervals of 500 milliseconds by default. Alternatively, you can specify the polling interval. An example of an explicit wait:

WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(5), Duration.ofMillis(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("...")));
element.click();

If the third parameter of the WaitDriverWait constructor is not specified, the default interval is used (which I already mentioned)


It should be obvious that, the slowest your expected load time for pages is, the better using an explicit wait will be for your tests. For example, if the expected load time for your pages is 10 seconds, but later on this improves to 2 seconds, you could continue using an explicit wait approach without changing your code. This is because, if the desired element is located within a second, it will not block the web driver for the rest of the wait duration (originally 10 seconds). If you use an implicit wait, you will need to change this if your load time improves.

The main drawback of using explicit waits is that you must specify it for each test and sometimes, for each web element interactions; whereas explicit wait need to be specified only once. Although, there is nothing preventing you from changing the amount of time to wait throughout your tests.

Also, keep in mind that implicit wait is global, whereas explicit wait must be defined per web element. That said, the same "wait" object can be passed to multiple elements as long as these elements have the same scope as the wait object. In my opinion, declaring global wait objects is a code smell. In principle, best coding practices call for limiting the scope of variables.


Fluent Wait

There is another type of wait called Fluent Wait. Fluent wait is a direct implementation of the Wait interface, and the parent class of WebDriveWait. In essence, FluentWait is also an explicit form of waiting.

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(Duration.ofSeconds(30L))
    .pollingEvery(Duration.ofSeconds(5L))
    .ignoring(NoSuchElementException.class);

WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
  public WebElement apply(WebDriver driver) {
    return driver.findElement(By.id("foo"));
  }

From the code snippet above, you should be able to see that using WebDriverWait over FluentWait seems to be an simpler to understand approach to explicitly waiting.

hfontanez
  • 5,774
  • 2
  • 25
  • 37