【问题标题】:Issue with WebElement.SendKeys(path): org.openqa.selenium.InvalidArgumentException: invalid argument: File not foundWebElement.SendKeys(路径)问题:org.openqa.selenium.InvalidArgumentException:无效参数:找不到文件
【发布时间】:2023-04-11 11:15:01
【问题描述】:

我在 Jenkins 上运行时使用 Seleinum 代码上传文件时遇到问题。我的 Selenium 代码在本地运行良好,能够上传并完成测试。但是,在 Jenkins 上运行时,WebElement.SendKeys(..) 抛出异常,上面写着:org.openqa.selenium.InvalidArgumentException: invalid argument: File not found"。我在代码中启动了一个新的 File(..)通过 file.exists() 测试文件是否存在,它确实存在。

driver.get(Configuration.getEnvArg("test_url")+"mylist/doc-upload");
String userDir = System.getProperty("user.dir");
String appendPath = File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + "files" + File.separator + fileName + "." + fileExt;
File file = new File(path);
LOGGER.info(path + " is exist: " + file.exists());
LOGGER.info("absolute path: " + file.getAbsolutePath());
WebElement el = driver.findElement(By.id(submitId));
el.sendKeys(file.getAbsolutePath());  // exception is thrown at this point.
pause(20);
(I replaced part of the path with xxx for posting purpose.)

invalid argument: File not found : /tmp/jenkins/workspace/xxxx/regression-tests/src/main/resources/files/dummy.png
20:49:30    (Session info: chrome=77.0.3865.75)
20:49:30  Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
20:49:30  System info: host: 'Unknown', ip: 'Unknown', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-1095-aws', java.version: '1.8.0_181'
20:49:30  Driver info: org.openqa.selenium.remote.RemoteWebDriver
20:49:30  Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 77.0.3865.75, chrome: {chromedriverVersion: 77.0.3865.40 (f484704e052e0..., userDataDir: /tmp/.com.google.Chrome.WI0TqR}, goog:chromeOptions: {debuggerAddress: localhost:46837}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
20:49:30  Session ID: 8c8f0738dbd321b5fb7ccb6a46adadf817b61366f1a9b4054586695693c69e5d
20:49:30  - com.qaprosoft.zafira.log.domain.MetaInfoMessage@236ded2c
20:49:30  - com.qaprosoft.zafira.log.domain.MetaInfoMessage@874bd76
20:49:30  - error processing.  Falling back to alternate logic.
20:49:30  org.openqa.selenium.InvalidArgumentException: invalid argument: File not found : /tmp/jenkins/workspace/xxxx/regression-tests/src/main/resources/files/dummy.png
20:49:30    (Session info: chrome=77.0.3865.75)
20:49:30  Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
20:49:30  System info: host: 'Unknown', ip: 'Unknown', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-1095-aws', java.version: '1.8.0_181'
20:49:30  Driver info: org.openqa.selenium.remote.RemoteWebDriver
20:49:30  Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 77.0.3865.75, chrome: {chromedriverVersion: 77.0.3865.40 (f484704e052e0..., userDataDir: /tmp/.com.google.Chrome.WI0TqR}, goog:chromeOptions: {debuggerAddress: localhost:46837}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
20:49:30  Session ID: 8c8f0738dbd321b5fb7ccb6a46adadf817b61366f1a9b4054586695693c69e5d
20:49:30    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
20:49:30    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
20:49:30    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
20:49:30    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
20:49:30    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
20:49:30    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
20:49:30    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
20:49:30    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
20:49:30    at com.qaprosoft.carina.core.foundation.webdriver.listener.EventFiringSeleniumCommandExecutor.execute(EventFiringSeleniumCommandExecutor.java:49)
20:49:30    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
20:49:30    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
20:49:30    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:106)
20:49:30    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
20:49:30    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
20:49:30    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
20:49:30    at java.lang.reflect.Method.invoke(Method.java:498)
20:49:30    at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement.lambda$new$0(EventFiringWebDriver.java:404)
20:49:30    at com.sun.proxy.$Proxy43.sendKeys(Unknown Source)
20:49:30    at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement.sendKeys(EventFiringWebDriver.java:429)

感谢任何建议。

谢谢, 格蕾丝

【问题讨论】:

  • 如果您的代码在本地运行,而不是在 Jenkins 上运行,那么这是文件路径本身的问题,与 Selenium 没有任何关系。我建议将您的本地文件路径与错误消息中的 Jenkins 文件路径进行比较,并尝试找出差异。问题是您的文件不在您输入的路径中。您可能需要编写自定义方法来获取动态文件路径。您的文件是否位于项目目录中?
  • 曾经定义过“路径”吗?我看到“userDir”和“appendPath”。
  • 这可能是权限问题造成的。请检查您或詹金斯工作是否具有读/写权限。
  • 回复:Christine,是的,该文件位于项目目录中。我做了一个新的 File(path).exists() 并且该文件确实存在。回复:@pcalkins,是的,我没有正确复制。 path 被定义为 String path = userDir + appendPath;。回复:Sureshmani,我会检查的。

标签: java jenkins testing filepath


【解决方案1】:

我找到了答案。 我没有使用sendKeys(..),而是使用核心方法中的ExtendedWebElement: 公共无效doAttachFile(String filePath)。它工作得很好。 据我了解,它会自动将LocalFileDetector 指定到驱动程序中,这允许它在运行代码的机器中而不是在远程机器中查找文件。 我的问题已经解决了。

【讨论】:

  • 嗨@mystackoverflow,您可以为您的解决方案提供代码吗?
猜你喜欢
  • 1970-01-01
  • 2019-10-03
  • 2015-04-09
  • 2015-08-25
  • 1970-01-01
  • 1970-01-01
  • 2017-08-17
  • 2012-06-26
  • 2015-01-25
相关资源
最近更新 更多