Selenium 4 - 无法使用 java selenium 4 中的发送键上传视频文件
Selenium 4 - Unable to upload a video file using send keys in java selenium 4
这是我的上传视频功能
public void uploadVideo(By locator, String path) {
WebElement chooseFile = driver.findElement(locator);
LocalFileDetector detector = new LocalFileDetector();
((RemoteWebElement) chooseFile).setFileDetector(detector);
chooseFile.sendKeys(path);
}
这是我在执行时遇到的错误,我在我的 selenium java 黄瓜框架中使用了上述函数。这曾经适用于 4 版本下的 selenium,因为我们已经升级到 selenium 4,我们面临这个问题。
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 100.0.4896.60, chrome: {chromedriverVersion: 100.0.4896.60 (6a5d10861ce8..., userDataDir: C:\Users\VISHAL~1\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:63997}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:63997/devtoo..., se:cdpVersion: 100.0.4896.60, 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}
Element: [[ChromeDriver: chrome on WINDOWS (c01e213dd495b9b7c3c4ef3845499e4e)] -> xpath: //div[@class='uppy-Dashboard-AddFiles']/input[@class='uppy-Dashboard-input'][2]]
Session ID: c01e213dd495b9b7c3c4ef3845499e4e
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
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:543)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:251)
at org.openqa.selenium.remote.RemoteWebElement.upload(RemoteWebElement.java:117)
at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:193)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1384)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:104)
at com.cucumber.utilities.DriverActions.uploadVideo(DriverActions.java:1034)
at com.peopleGrove.pageActions.QnAPromptsPageActions.videoPromptCreatorPage_dragandUploadVideo(QnAPromptsPageActions.java:229)
at com.peopleGrove.stepDefinitions.QnAPromptsStepDefinitions.upload_the_pre_created_video_from_the_files(QnAPromptsStepDefinitions.java:224)
这个错误信息...
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 100.0.4896.60, chrome: {chromedriverVersion: 100.0.4896.60 (6a5d10861ce8..., userDataDir: C:\Users\VISHAL~1\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:63997}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:63997/devtoo..., se:cdpVersion: 100.0.4896.60, 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}
Element: [[ChromeDriver: chrome on WINDOWS (c01e213dd495b9b7c3c4ef3845499e4e)] -> xpath: //div[@class='uppy-Dashboard-AddFiles']/input[@class='uppy-Dashboard-input'][2]]
...表示虽然元素已识别但 chooseFile.sendKeys(path)
失败。
解决方案
等待 <input>
元素 可点击 ,然后按如下所示尝试发送文件:
public void uploadVideo(By locator, String path) {
WebElement chooseFile = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(locator));
LocalFileDetector detector = new LocalFileDetector();
((RemoteWebElement) chooseFile).setFileDetector(detector);
chooseFile.sendKeys(path);
}
这有效!!!!
public void uploadVideo(By locator, String path) throws InterruptedException {
Thread.sleep(2000);
WebElement chooseFile = driver.findElement(locator);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].style.display='block';", chooseFile);
chooseFile.sendKeys(path);
}
这是我的上传视频功能
public void uploadVideo(By locator, String path) {
WebElement chooseFile = driver.findElement(locator);
LocalFileDetector detector = new LocalFileDetector();
((RemoteWebElement) chooseFile).setFileDetector(detector);
chooseFile.sendKeys(path);
}
这是我在执行时遇到的错误,我在我的 selenium java 黄瓜框架中使用了上述函数。这曾经适用于 4 版本下的 selenium,因为我们已经升级到 selenium 4,我们面临这个问题。
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 100.0.4896.60, chrome: {chromedriverVersion: 100.0.4896.60 (6a5d10861ce8..., userDataDir: C:\Users\VISHAL~1\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:63997}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:63997/devtoo..., se:cdpVersion: 100.0.4896.60, 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}
Element: [[ChromeDriver: chrome on WINDOWS (c01e213dd495b9b7c3c4ef3845499e4e)] -> xpath: //div[@class='uppy-Dashboard-AddFiles']/input[@class='uppy-Dashboard-input'][2]]
Session ID: c01e213dd495b9b7c3c4ef3845499e4e
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
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:543)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:251)
at org.openqa.selenium.remote.RemoteWebElement.upload(RemoteWebElement.java:117)
at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:193)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1384)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:104)
at com.cucumber.utilities.DriverActions.uploadVideo(DriverActions.java:1034)
at com.peopleGrove.pageActions.QnAPromptsPageActions.videoPromptCreatorPage_dragandUploadVideo(QnAPromptsPageActions.java:229)
at com.peopleGrove.stepDefinitions.QnAPromptsStepDefinitions.upload_the_pre_created_video_from_the_files(QnAPromptsStepDefinitions.java:224)
这个错误信息...
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 100.0.4896.60, chrome: {chromedriverVersion: 100.0.4896.60 (6a5d10861ce8..., userDataDir: C:\Users\VISHAL~1\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:63997}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:63997/devtoo..., se:cdpVersion: 100.0.4896.60, 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}
Element: [[ChromeDriver: chrome on WINDOWS (c01e213dd495b9b7c3c4ef3845499e4e)] -> xpath: //div[@class='uppy-Dashboard-AddFiles']/input[@class='uppy-Dashboard-input'][2]]
...表示虽然元素已识别但 chooseFile.sendKeys(path)
失败。
解决方案
等待 <input>
元素 可点击 ,然后按如下所示尝试发送文件:
public void uploadVideo(By locator, String path) {
WebElement chooseFile = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(locator));
LocalFileDetector detector = new LocalFileDetector();
((RemoteWebElement) chooseFile).setFileDetector(detector);
chooseFile.sendKeys(path);
}
这有效!!!!
public void uploadVideo(By locator, String path) throws InterruptedException {
Thread.sleep(2000);
WebElement chooseFile = driver.findElement(locator);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].style.display='block';", chooseFile);
chooseFile.sendKeys(path);
}