3

I am working with selenium automation and I am not able to upload file with relative path please see the below code.

driver.findElement(By.xpath("//span[text()='Theme']")).click();

File filepath=new File("\ntwinelogin.jpg");
WebElement fileobj = driver.findElement(By.name("toplogoupload"));
fileobj.sendKeys("\ntwinelogin.jpg");
String Filepath=filepath.getAbsolutePath();
Filepath.trim();
McGrady
  • 10,869
  • 13
  • 47
  • 69
Manoj Kumar
  • 43
  • 1
  • 1
  • 9

4 Answers4

3

Use System.getProperty("user.dir") for your current project directory path.

System.getProperty("user.dir")+"\ntwinelogin.jpg";
Ashish Deshmukh
  • 448
  • 4
  • 17
1

You can use below function for relative path in Python:

Import

import sys, os

Use code as below :

ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
screenshotpath = os.path.join(os.path.sep, ROOT_DIR,'YOURFOLDERNAME'+ os.sep)
print screenshotpath

make sure you create folder where .py file is present

Java

System.getProperty("user.dir")
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
0

Step 1 : First store the file in one variable e.g. String path = "C:\\users\\home\\newhtml.html";

Step 2 : Pass the variable to sendkeys() method

driver.findelement(By.xapth("")).sendkeys(path);
NarendraR
  • 7,577
  • 10
  • 44
  • 82
0

I recently required a platform-agnostic solution to this problem. Since we were using Maven, I implemented the following solution.

Maven directory structure

src
 |
  - main
 |    |
 |     - java
 |         |
 |          - com
 |             |
 |              - example
 |                   |
 |                    - integration
 |                            |
 |                             - Test1.java
 |                            |
 |                             - Test2.java
 |                            ...
 |
  - resources
        |
         - File1.csv
        |
         - File2.csv
        ...

Test

// Load a file on the classpath as a resource using the ClassLoader.
URL    url  = getClass().getClassLoader().getResource("File1.csv");

// Convert the URL into a URI.
URI    uri  = file.toURI();

// Load the file from the URI.
File   file = new File(uri);

// Get the absolute path to the file.
String path = file.getAbsolutePath();

// Use the absolute path with Selenium.
input.sendKeys(path);
manish
  • 19,695
  • 5
  • 67
  • 91