"path is not absolute" exception while uploading file in selenium
Asked Answered
B

5

5

path is not absolute: src/test/resources/testData/twt_Pic.jpg getting this exception while uploading the file using selenium.

Instead of uploading the file using the native os file explorer, sending the keys to the path file.

The same path used to work during the older version, not sure which version was it.

Error Log in 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)

Error Log for 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

Project Structure:

  1. Project/src/main/java - Contains actions, UI element repository, Step Defenitions. From where the file is being called
  2. Project/src/test/java - Contains only runner class
  3. Project/src/test/resources/customLib - Contains drivers
  4. Project/src/test/resources/features - Contains feature files
  5. Project/src/test/resources/testData - Contains the test data including the file which i'm trying to call here.
Boutique answered 4/6, 2018 at 15:51 Comment(2)
Add more detailsPhenothiazine
@Prany, uploading the file using the method of sending the path of the file using "sendKeys" methodBoutique
E
8

You can try creating a File instance and getting the absolute path from there.

File file = new File("src/test/resources/testData/twt_Pic.jpg");
yourElement.sendKeys(file.getAbsolutePath());
Errata answered 5/6, 2018 at 3:23 Comment(0)
O
2

I had the same error in python and I solved with this:

abs_path = os.path.abspath("relative_path_to_file.jpg")
driver.find_element(By.TAG_NAME,"input").send_keys(abs_path)
Osculation answered 1/11, 2023 at 23:31 Comment(0)
A
1

the error message is clearly telling you what's wrong.

you are trying to upload src/test/resources/testData/twt_Pic.jpg, which is a relative path to an image. As the error states, it must be an absolute path (not relative). Replace the path and try again.

Astrophotography answered 4/6, 2018 at 17:35 Comment(2)
this exact path was working in an older version of selenium. but trying to understand and a workaround so that when the code runs on different machines, I do not have to look for the paths. As src/test/resources/testData/twt_Pic.jpg resides in the project, it will be easyBoutique
convert it to an absolute path in your code, then pass the absolute path to sendKeys()Astrophotography
F
0

As (possibly) you are using Maven you need to provide the relative path with respective your Maven Project directory as follows:

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

Where . refers to the Parent Project Directory

Frier answered 4/6, 2018 at 17:50 Comment(4)
Can you clean up the Project space through Clean & Rebuild All through your IDE and retest?Frier
still getting the unknown error: path is not absolute: ./src/test/resources/testData/twt_Pic.jpgBoutique
@NagarjunaReddy Can you update the question with the directory hierarchy/structure of /testData/twt_Pic.jpg from Project Level?Frier
Updated the project structure. Please check if it helpsBoutique
R
0

This worked for me

String path = "src/test/resources/testData/twt_Pic.jpg";
File file = new File(new File(path).getAbsolutePath());
Ravine answered 22/11, 2019 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.