0

I'm writing an automation script for one project, there's a task where I need to upload a file from a local driver to the browser. So can someone please help me out!

Image Description Here

barbsan
  • 3,418
  • 11
  • 21
  • 28
GirisH
  • 1
  • 1
  • Possible duplicate of [How to upload file using Selenium WebDriver in Java](https://stackoverflow.com/questions/16896685/how-to-upload-file-using-selenium-webdriver-in-java) – Mate Mrše May 20 '19 at 09:53

2 Answers2

1

According to your image I can see, there is only one file. testexcel.xlxs. So what you can do is you can specify the file path. You haven't posted the HTML Code and the Programming Language that you are trying to implement this. The following code snippet is written in Java just to take an idea.

    // Get the input field id
    WebElement uploadElement = driver.findElement(By.id("uploadfile"));

    // Enter the file path onto the file-selection input field
    uploadElement.sendKeys("H:\\Excelfile\\testexcel.xlxs");

    // Click the "SUBMIT" button
    driver.findElement(By.name("submit")).click();
Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36
0

Sendkeys is one of method to upload file and another method.

Robot Class in a separate class which is part of Java not a part of Selenium, Robot class is mainly created for automating Java Platform implementation. The primary purpose of Robot is to facilitate automated testing of Java platform implementations. In simple terms, the class provides control over the mouse and keyboard devices.

driver.findElement(By.xpath("Path of that element")).click();

StringSelection strSel = new StringSelection("upload file path");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(strSel, null);


Robot robot = new Robot();

robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);

Thread.sleep(3000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

for more information go through this link.

There is another ways also to upload file go through this link https://www.evoketechnologies.com/blog/selenium-automation-uploading-multiple-files-via-web-browsers-file-dialog/

check this answer https://stackoverflow.com/a/56168803/4513879

Pradnya Bolli
  • 1,915
  • 1
  • 19
  • 37