执行Selenium代码触发InvalidArgumentException错误求助
Selenium ChromeDriver 点击上传元素时的 InvalidArgumentException 错误处理
错误信息
Exception in thread "main" org.openqa.selenium.InvalidArgumentException: 无效参数 (Session info: chrome=103.0.5060.114) Build info: version: '4.3.0', revision: 'a4995e2c09*' 系统信息:主机:'SADAF-RAUF',ip:'192.168.1.55',os.name: 'Windows 10',os.arch: 'amd64',os.version: '10.0',java.version: '11.0.15' 驱动信息:org.openqa.selenium.chrome.ChromeDriver 命令:[99bdbf5c25fd5790a4c9901c3f7f1dde, clickElement {id=1d9cbee0-b4cb-4993-b271-3a3e36c42165}] Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 103.0.5060.114, chrome: {chromedriverVersion: 103.0.5060.53 (a1711811edd7..., userDataDir: C:\Users\Sadaf\AppData\Loca...}, goog:chromeOptions: {debuggerAddress: localhost:64336}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:64336/devtoo..., se:cdpVersion: 103.0.5060.114, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true} 元素:[[ChromeDriver: chrome on WINDOWS (99bdbf5c25fd5790a4c9901c3f7f1dde)] -> id: uploadPicture] 会话ID:99bdbf5c25fd5790a4c9901c3f7f1dde 错误堆栈: at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) 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:184) at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:569) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:257) at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:78) at com.autoit.FileUpload.upload(FileUpload.java:25) at com.autoit.FileUpload.main(FileUpload.java:32)
常见原因及解决方法
- 元素未就绪:元素可能未完全加载或处于不可点击状态,添加显式等待确保元素可交互:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); WebElement uploadBtn = wait.until(ExpectedConditions.elementToBeClickable(By.id("uploadPicture"))); uploadBtn.click();
- 错误的上传方式:对于
input[type="file"]类型的上传元素,无需点击弹窗,直接用sendKeys传入文件路径更稳定:
WebElement uploadElement = driver.findElement(By.id("uploadPicture")); uploadElement.sendKeys("C:\\你的文件路径\\文件名.jpg");
版本不兼容:ChromeDriver与Chrome浏览器版本需严格匹配(当前Chrome为103.0.5060.114,ChromeDriver为103.0.5060.53,小版本差异可能引发问题),下载对应版本的ChromeDriver即可。
元素隐藏:若目标元素是隐藏状态,可通过JavaScript先使其可见再操作:
WebElement uploadElement = driver.findElement(By.id("uploadPicture")); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].style.display = 'block';", uploadElement); uploadElement.click();
内容的提问来源于stack exchange,提问作者Sadaf Rauf
相关产品推荐
相关产品推荐

