我正在按照本教程在此页面上练习 Selenium。我正在使用最新版本的 Selenium (4.26)、TestNG (7.10.2 和 Java JDK (Java 23) 以及 IntellJ 社区版。
在学习本教程时,我注意到当我启动此方法时,测试继续给出“无此类元素异常”:
private By uploadButton= By.xpath("//*[@id='file-submit']");
public void clickUploadButton(){
WebElement button= driver.findElement(uploadButton);
button.click();
}
在使用 XPath 之前,我尝试过使用 id 和 class,但没有任何结果。我什至采用了隐式的方式,但没有任何改变。
奇怪的部分是,如果我尝试在元素上使用“isDisplayed”方法在单击之前,它会返回 true。所以我不明白为什么在测试过程中找不到这个元素结果。
我把所有代码和答案放在这里:
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
尝试使用 JavaScriptExecutor 单击按钮。请参阅下面的代码:
WebElement button= driver.findElement(uploadButton);
//button.click();
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
注意:通常,当 Selenium 无法执行所需操作时,应使用 JS 作为最后的手段。