在Chrome 73中将sendKeys(fileAbsolutePath)用作<input type =“ file” />时,如何解决文件上传错误?

时间:2019-04-22 04:26:15

标签: java google-chrome selenium-webdriver file-upload input-type-file

我有一个Selenium WebDriver / JUnit测试,该测试通过<input type="file">元素在Windows 10中测试文件上传。

File file = new File(filePath);
driver.findElement(By.xpath("//input[@type='file']")).sendKeys(file.getAbsolutePath());

Browse按钮的HTML:

<label _ngcontent-cxx="" class="modal-button" for="file"> Browse 
    <input _ngcontent-cxx="" accept=".docx, .png, .pdf, .xlsx, .doc, .jpg" class="display-none" id="file" name="file" type="file">
</label>

该测试在Chrome 72之前运行良好,但是最近更新为Chrome 73和chromedriver 73.0.3683.68之后,文件上传无法正常进行。 UI中显示错误Upload failed,并且上载请求(POST)返回状态码400。仅在运行自动测试时才会发生。手动上传效果很好。

通过Chromium issue 792336链接的GitHub issue 3730 - File upload on input, stop working in Chrome 73进行了快速的Google搜索。

File Inputs: Don't generate change events when 'files' is set

The <input type=file> element generates "change" events when the user
interacts with the control to select files, exposed as the 'files'
property (a FileList). This property can be assigned to by script, and
should not generate a "change" event in such a case. It was
erroneously doing so, so fix it.

我不确定自己是否了解,但这是否意味着以后自动文件上传将不再在Chrome中工作?还是有一种方法可以使通过<input type="file">上传的文件在Chrome 73中正常工作?

注意:我也尝试使用Robot类通过本机Windows上传窗口上传文件,但是文件上传失败,并出现相同的错误。

StackTrace (在上传步骤之后的验证步骤中):

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //table[@id='table1']//*[contains(text(), 'license.pdf')] (tried for 8 second(s) with 500 milliseconds interval)
        at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
        at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
        at pageObjects.BasePage.waitForElementToAppear(BasePage.java:116)
        at pageObjects.BasePage.uploadFile(BasePage.java:771)
        at stepDefinitions.testStepDefs.enterNameAndUploadLicenseDocument(testStepDefs.java:306)
        at ?.I enter the name and upload a license document(file:target/parallel/features/test_scenario001_run001_IT.feature:22)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//table[@id='table1']//*[contains(text(), 'license.pdf
')]"}
  (Session info: chrome=73.0.3683.103)
  (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'host1', ip: '10.21.171.143', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_161'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 73.0.3683.68 (47787ec04b6e3..., userDataDir: C:\Users\user1\AppData\L...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:XXXXX}, 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}, unexpected
AlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 73.0.3683.103, webStorageEnabled: true}
Session ID: 7de258a01764adc064e8c966xx77017g
*** Element info: {Using=xpath, value=//table[@id='table1']//*[contains(text(), 'license.pdf')]}
        at sun.reflect.GeneratedConstructorAccessor12.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        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.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
        at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
        at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
        at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205)
        at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:201)
        at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
        at pageObjects.BasePage.waitForElementToAppear(BasePage.java:116)
        at pageObjects.BasePage.uploadFile(BasePage.java:771)
        at stepDefinitions.testStepDefs.enterNameAndUploadLicenseDocument(testStepDefs.java:306)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at cucumber.runtime.Utils$1.call(Utils.java:26)
        at cucumber.runtime.Timeout.timeout(Timeout.java:16)
        at cucumber.runtime.Utils.invoke(Utils.java:20)
        at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:57)
        at cucumber.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:50)
        at cucumber.runner.TestStep.executeStep(TestStep.java:65)
        at cucumber.runner.TestStep.run(TestStep.java:50)
        at cucumber.runner.PickleStepTestStep.run(PickleStepTestStep.java:43)
        at cucumber.runner.TestCase.run(TestCase.java:46)
        at cucumber.runner.Runner.runPickle(Runner.java:49)
        at cucumber.runtime.junit.PickleRunners$NoStepDescriptions.run(PickleRunners.java:146)
        at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:68)
        at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:23)
        at org.junit.runners.ParentRunner$4.run(ParentRunner.java:330)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:78)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:328)
        at org.junit.runners.ParentRunner.access$100(ParentRunner.java:65)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:292)
        at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:412)
        at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:73)
        at cucumber.api.junit.Cucumber.runChild(Cucumber.java:123)
        at cucumber.api.junit.Cucumber.runChild(Cucumber.java:65)
        at org.junit.runners.ParentRunner$4.run(ParentRunner.java:330)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:78)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:328)
        at org.junit.runners.ParentRunner.access$100(ParentRunner.java:65)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:292)
        at cucumber.api.junit.Cucumber$RunCucumber.evaluate(Cucumber.java:147)
        at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:412)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)

1 个答案:

答案 0 :(得分:0)

Chrome 74 今天发布,这不再是问题。通过<input type="file">使用sendKeys()进行上传再次与Chrome 74/ChromeDriver 74.0.3729.6一起使用。