I am praticing Selenium following this tutorial working on this page. I am using the more recent version of Selenium (4.26), TestNG (7.10.2 and of Java JDK (Java 23) with IntellJ Community edition.
While I was following the tutorial I have noticing that a test continue to give me "No such element exception" when I launch this method:
private By uploadButton= By.xpath("//*[@id='file-submit']");
public void clickUploadButton(){
WebElement button= driver.findElement(uploadButton);
button.click();
}
Before to use the XPath, I've tried with id and class but nothing. I've even put an implicit way but nothing change.
The strange part is if I try to use "isDisplayed" method on the element before the click, it returns me true. So I don't understand why during the test this element results not found.
I put all the code and the answer here:
public void clickUploadButton(){
WebElement button= driver.findElement(uploadButton);
System.out.println("the element is "+button.isDisplayed());
button.click();
}
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
nov 12, 2024 10:34:57 AM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
WARNING: Unable to find CDP implementation matching 130
nov 12, 2024 10:34:57 AM org.openqa.selenium.chromium.ChromiumDriver lambda$new$5
WARNING: Unable to find version of CDP to use for 130.0.6723.117. 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.26.0` 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.
the element is true
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".button"}
(Session info: chrome=130.0.6723.117)
For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Build info: version: '4.26.0', revision: '8ccf0219d7'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '23'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [86364529612327c68c95c4fba0a9bb2c, findElement {using=class name, value=button}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 130.0.6723.117, chrome: {chromedriverVersion: 130.0.6723.116 (6ac35f94ae3..., userDataDir: C:\Users\CARLOC~1\AppData\L...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:52980}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: windows, proxy: Proxy(), se:cdp: ws://localhost:52980/devtoo..., se:cdpVersion: 130.0.6723.117, 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: 86364529612327c68c95c4fba0a9bb2c
Try to click the button using JavaScriptExecutor. See code below:
WebElement button= driver.findElement(uploadButton);
//button.click();
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Imports:
import org.openqa.selenium.JavascriptExecutor;
Note: Generally one should use JS as a last resort when Selenium is not able to perform the desired action.