Search code examples
selenium-webdriverselenium-java

Find Element by id with selenium java


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class SeleniumBasics {
    
public static void main(String[] args) throws InterruptedException {  
        
        WebDriver driver= new ChromeDriver();  
        driver.get("https://formy-project.herokuapp.com/");
        driver.manage().window().maximize(); 
        driver.findElement(By.xpath("//a[@class='btn btn-lg' and text()='Autocomplete']")).click();
        WebElement autocomplete = driver.findElement(By.id("autocomplete"));
        autocomplete.sendKeys("1555 Park Blvd, Palo Alto, CA");
        Thread.sleep(1000);
        
    }  

}

In Google Chrome Open, clicking on autocomlete works fine, then I got an error here WebElement autocomplete = driver.findElement(By.id("autocomplete"));

Here is the error I get:

juin 27, 2024 2:43:17 P.M. org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
WARNING: Unable to find CDP implementation matching 126
juin 27, 2024 2:43:17 P.M. org.openqa.selenium.chromium.ChromiumDriver lambda$new$5
WARNING: Unable to find version of CDP to use for 126.0.6478.115. You may need to include a dependency on a specific version of the CDP using something similar to `org.seleniumhq.selenium:selenium-devtools-v86:4.16.1` where the version ("v86") matches the version of the chromium-based browser you're using and the version number of the artifact is the same as Selenium's.
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#autocomplete"}
  (Session info: chrome=126.0.6478.115)
For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Build info: version: '4.16.1', revision: '9b4c83354e'
System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '22'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [b7a6e061f82aac92c3ac81d291082ca6, findElement {using=id, value=autocomplete}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 126.0.6478.115, chrome: {chromedriverVersion: 126.0.6478.126 (d36ace6122e..., userDataDir: C:\Users\Kamilia\AppData\Lo...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:52425}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: windows, proxy: Proxy(), se:cdp: ws://localhost:52425/devtoo..., se:cdpVersion: 126.0.6478.115, 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: b7a6e061f82aac92c3ac81d291082ca6
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
    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:52)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:191)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:200)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:175)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:523)
    at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:165)
    at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:66)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:354)
    at SeleniumBasics.main(SeleniumBasics.java:15)

Find element by Id,Xpath


Solution

  • The reason for no such element exception is exactly what is mentioned in the other answer. That is, you haven't defined a waiting strategy.

    Here is the refactored code which uses Explicit waits to locate elements:

    WebDriver driver= new ChromeDriver();  
    driver.get("https://formy-project.herokuapp.com/");
    driver.manage().window().maximize(); 
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
            
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='btn btn-lg' and text()='Autocomplete']"))).click();        
    wait.until(ExpectedConditions.elementToBeClickable(By.id("autocomplete"))).sendKeys("1555 Park Blvd, Palo Alto, CA");
    Thread.sleep(1000);