jenkins WebElement.SendKeys(path)的问题:org.openqa.selenium.InvalidArgumentException:无效参数:文件未找到

r7xajy2e  于 2023-04-20  发布在  Jenkins
关注(0)|答案(2)|浏览(107)

在Jenkins上运行时,我使用Seleinum代码上传文件时遇到问题。我的Selenium代码在本地工作正常,能够上传并完成测试。然而,在Jenkins上运行时,WebElement.SendKeys(..)抛出异常,显示:org.openqa.selenium.InvalidArgumentException:无效参数:我在代码中启动了一个新的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)

任何建议都很感激。
谢谢你,格蕾丝

xmq68pz9

xmq68pz91#

我找到了答案。我没有使用sendKeys(..),而是使用了ExtendedWebElement from core方法:public void doAttachFile(String filePath)。它工作得很好。根据我的理解,它自动将LocalFileDetector指定到驱动程序中,这允许它在运行代码的机器中找到文件,而不是在远程机器中。我的问题解决了。

6l7fqoea

6l7fqoea2#

我有同样的问题,但@mystackoverflow的答案和下面的页面https://www.tabnine.com/code/java/classes/org.openqa.selenium.remote.LocalFileDetector引导我找到了解决方案。
必须设置驱动程序localfiledetector:

driver.setFileDetector(new LocalFileDetector());

文件上传的更多信息和代码示例:https://www.selenium.dev/documentation/webdriver/drivers/remote_webdriver/#local-file-detector

相关问题