在使用Selenium上传文件时出现“路径不是绝对路径”异常。

5

路径不是绝对路径:src/test/resources/testData/twt_Pic.jpg 在使用Selenium上传文件时出现此异常。

与其使用本地操作系统的文件浏览器上传文件,不如将路径文件发送给键。

同样的路径在旧版本中可以使用,但不确定是哪个版本。

Chrome中的错误日志:

org.openqa.selenium.WebDriverException: unknown error: path is not absolute: src/test/resources/testData/twt_Pic.jpg
  (Session info: chrome=66.0.3359.181)
  (Driver info: chromedriver=2.39.562713 (dd642283e958a93ebf6891600db055f1f1b4f3b2),platform=Mac OS X 10.13.4 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'nagarjunaMBP.local', ip: 'fe80:0:0:0:c2e:b816:67ae:922b%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.4', java.version: '1.8.0_60'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.39.562713 (dd642283e958a9..., userDataDir: /var/folders/g4/dylg4g7s7wb...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 66.0.3359.181, webStorageEnabled: true}
Session ID: bdf391bf16ddbda6c9f73d559404bae7
    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:422)
    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:543)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:276)
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:100)
    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:497)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:51)
    at com.sun.proxy.$Proxy17.sendKeys(Unknown Source)
    at actions.PublishBroadcast.MediaLibAction.uploadImage(MediaLibAction.java:43)
    at stepDefinitions.Publish.SDMediaLibrary.Upload_an_Image(SDMediaLibrary.java:43)
    at ✽.When Upload an Image(features/publish/mediaLibrary/MediaLibrary.feature:11)

Firefox的错误日志:

org.openqa.selenium.InvalidArgumentException: File not found: src/test/resources/testData/twt_Pic.jpg
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'nagarjunaMBP.local', ip: 'fe80:0:0:0:c2e:b816:67ae:922b%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.4', java.version: '1.8.0_60'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 60.0.1, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: false, moz:processID: 53177, moz:profile: /var/folders/g4/dylg4g7s7wb..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: MAC, platformName: MAC, platformVersion: 17.5.0, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: 7c932de3-ea6a-a143-a7b4-bf7dfa3e2660

项目结构:

  1. Project/src/main/java - 包含操作、UI元素库和步骤定义。从哪里调用文件。
  2. Project/src/test/java - 仅包含运行类
  3. Project/src/test/resources/customLib - 包含驱动程序
  4. Project/src/test/resources/features - 包含功能文件
  5. Project/src/test/resources/testData - 包含测试数据,包括我在这里尝试调用的文件。

添加更多细节 - Prany
@Prany,使用“sendKeys”方法发送文件路径的方式上传文件。 - Nagarjuna Reddy
5个回答

6
你可以尝试创建一个File实例并从中获取绝对路径。
File file = new File("src/test/resources/testData/twt_Pic.jpg");
yourElement.sendKeys(file.getAbsolutePath());

1
错误信息清楚地告诉您问题出在哪里。
您正在尝试上传相对路径为src/test/resources/testData/twt_Pic.jpg的图像。正如错误所述,它必须是绝对路径(而不是相对路径)。替换路径并重试。

这个确切的路径在旧版本的Selenium中是有效的。但是我正在尝试理解并找到一种解决方法,以便当代码在不同的机器上运行时,我不必寻找路径。由于src/test/resources/testData/twt_Pic.jpg位于项目中,因此这将很容易。 - Nagarjuna Reddy
将其转换为代码中的绝对路径,然后将绝对路径传递给sendKeys()函数。 - Corey Goldberg

0

如果您正在使用 Maven,则需要按照以下方式提供相对路径,与您的 Maven 项目 目录相关:

./src/test/resources/testData/twt_Pic.jpg

. 表示 父项目目录


你能不能通过IDE的CleanRebuild All来清理一下_Project_空间,并重新测试一下? - undetected Selenium
仍然出现“未知错误:路径不是绝对路径:./src/test/resources/testData/twt_Pic.jpg” - Nagarjuna Reddy
@NagarjunaReddy,你能否更新问题,提供/testData/twt_Pic.jpg在项目层级下的目录结构? - undetected Selenium
已更新项目结构,请检查是否有所帮助。 - Nagarjuna Reddy

0

这对我有用

String path = "src/test/resources/testData/twt_Pic.jpg";
File file = new File(new File(path).getAbsolutePath());

0
我在Python中遇到了同样的错误,我用以下方法解决了:
abs_path = os.path.abspath("relative_path_to_file.jpg")
driver.find_element(By.TAG_NAME,"input").send_keys(abs_path)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接