这是我的上传视频功能
public void uploadVideo(By locator, String path) {
WebElement chooseFile = driver.findElement(locator);
LocalFileDetector detector = new LocalFileDetector();
((RemoteWebElement) chooseFile).setFileDetector(detector);
chooseFile.sendKeys(path);
}
这是我在执行时遇到的一个错误,我正在硒java黄瓜框架中使用上面的函数。这曾经适用于4版本下的selenium,因为我们已经升级到selenium 4,所以我们面临这个问题。
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 100.0.4896.60, chrome: {chromedriverVersion: 100.0.4896.60 (6a5d10861ce8..., userDataDir: C:UsersVISHAL~1AppDataL...}, 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$3$1.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)
成功了!!!!
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);
}
此错误消息。。。
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 100.0.4896.60, chrome: {chromedriverVersion: 100.0.4896.60 (6a5d10861ce8..., userDataDir: C:UsersVISHAL~1AppDataL...}, 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]]
意味着尽管元素被识别,但是CCD_ 1失败。
解决方案
等待<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);
}