javaselenium-webdriverexceptionnosuchelementexception

Searching the xpath in html code, highlights the exact path which is need, but inside the code it is not working


Here is the exception I am getting while running the code:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(.,'STEP3 Orientation')]"}
  (Session info: chrome=115.0.5790.110)
For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Build info: version: '4.10.0', revision: 'c14d967899'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.7'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [3b7c5c64e8bf5a509fa70a6b5f095c21, findElement {using=xpath, value=//span[contains(.,'STEP3 Orientation')]}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 115.0.5790.110, chrome: {chromedriverVersion: 114.0.5735.90 (386bc09e8f4f..., userDataDir: C:\Users\User\AppData\Local...}, goog:chromeOptions: {debuggerAddress: localhost:61749}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: windows, proxy: Proxy(), se:cdp: ws://localhost:61749/devtoo..., se:cdpVersion: 115.0.5790.110, 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: 3b7c5c64e8bf5a509fa70a6b5f095c21
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:199)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:132)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:51)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:191)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:196)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:171)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:531)
    at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:165)
    at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:59)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:350)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
    at Selenium.Demo1.main(Demo1.java:30)


I am trying to fetch the details using the following xpath:
//driver.findElement(By.xpath("//span[contains(text(),'STEP3 Orientation')]")).getText();
//driver.findElement(By.xpath("//span[contains(@class,'pageTitle')]")).getText();
//driver.findElement(By.xpath("//span[contains(.,'STEP3 Orientation')]")).getText();

For all 3 xpath, I am getting the NoSuchElementException

html code Searching the xpath in html code, highlights the exact path which is need Searching the xpath in html code, highlights the exact path which is need, but inside the code it is not working.


Solution

  • Try applying selenium's Waits. Below code uses ExplicitWait to locate the desired element:

    WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(30));
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(text(),'STEP3 Orientation')]"))).getText();
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(@class,'pageTitle')]"))).getText();
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(.,'STEP3 Orientation')]"))).getText();
    

    Imports:

    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;