我尝试上传文件,但引发异常。上传按钮是自定义的。我什至试图单击它,但它卡在那里。
new WebDriverWait(driver,100).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='divProfileSetting']/div/div/div/div[7]/div[2]/div/div/div/label/span")));
WebElement UploadingFile1 = driver.findElement(By.xpath("//div[@id='divProfileSetting']/div/div/div/div[7]/div[2]/div/div/div/label/span"));
//UploadingFile1.click();
UploadingFile1.sendKeys("E:\\Hatha.jpg");
例外:
Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable
(Session info: chrome=74.0.3729.108)
(Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'RAUNAK-MA', ip: '172.27.242.131', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.46.628402 (536cd7adbad73a..., userDataDir: C:\Users\RAUNAK~1.MAS\AppDa...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:55361}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), rotatable: false, setWindowRect: true, strictFileInteractability: false, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 74.0.3729.108, webStorageEnabled: true}
Session ID: 53e0b557906a50d6f51f9aa0c2bf1a14
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:106)
at newpackage1.flightdeckau.main(flightdeckau.java:96)
HTML:
<div class="span12 logouploadContainer">
<input type="file" id="file" name="file" tabindex="-1" style="position: fixed; left: -9999px;">
<div class="bootstrap-filestyle" style="display: inline;" tabindex="0">
<input type="text" class="input-large" disabled="">
<label for="file" class="btn btn-primary">
<i class=" icon-white icon-folder-open" data-original-title="" title=""></i>
<span data-original-title="" title="">Choose File</span>
</label>
</div>
<div id="logo-div" class="hidden">
<button type="button" id="btnResetLogo" class="btn" style="margin-left: 5px;" data-original-title="" title="">Remove</button>
<div id="imgContainer" style="height: 100%; width: 100px; padding: 5px; overflow: hidden;">
<img alt="Client Logo" id="imgClient" src="">
</div>
</div>
</div>
也尝试过等待元素,但似乎有一些问题。
答案 0 :(得分:3)
只有与<input type=file>
(https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file)结合使用时,尝试使用.sendKeys()上传文件才有效。
查看您的代码,您正在尝试将文件信息发送到<span>
元素。
代替尝试:
WebDriverWait wait = new WebDriverWait(driver, 15, 100);
WebElement uploadFileElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("file")));
uploadFileElement.sendKeys("E:\\Hatha.jpg");
这将等待<input type="file">
元素变为可见,然后将使用sendKeys()发送文件。如果<input type="file">
永远不可见,这将不起作用,如果是这种情况,您可以通过使用JavaScript使它可见来解决此问题,但这将是一个hack,不能代表最终用户的操作。
*编辑*
如果您决定采用JavaScript hack路线,则可以执行以下操作:
WebDriverWait wait = new WebDriverWait(driver, 15, 100);
WebElement uploadFileElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("file")));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].style.visibility='visible'", uploadFileElement);
uploadFileElement.sendKeys("E:\\Hatha.jpg");
请注意,现在的预期条件是等待元素存在于DOM中,而不是等待其可见,然后在使用sendKeys()与之交互之前,我们使用JavaScript显式使该元素可见。
您可能不需要将diver对象转换为JavascriptExecutor。如果您有RemoteWebDriver,ChromeDriver或FirefoxDriver实例而不是WebDriver实例,则该方法已经可用。
* EDIT 2 *
再次查看此问题,真正的问题是<input type="file">
元素已被推离屏幕左侧。因此,修复程序是上述内容的变体。除了强制元素不可见之外,我们还可以使用JavaScript强制将偏移量设置为0,而不是-9999px:
WebDriverWait wait = new WebDriverWait(driver, 15, 100);
WebElement uploadFileElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("file")));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].style.left='0'", uploadFileElement);
uploadFileElement.sendKeys("E:\\Hatha.jpg");
答案 1 :(得分:1)
ElementNotInteractableException
是在找到元素时引起的,但您无法与其交互。例如,您可能无法单击或发送密钥。
这可能有几个原因:
解决方案
等待直到元素可见/可点击
我看到您已经添加了wait
,但已将其配置为仅等到100ms
至timeout
。因此,如果元素不是interactable
中的100ms
,则等待将结束。尝试将其至少增加到1 second i.e 1000ms
或它取决于站点的速度。