Not able to upload file using sendKeys() – Please help

Hi,

 I need to upload a file. There are 2 options :
1. Select file input field and sendKeys()
2. AutoIT
1. sendKeys() : I am using below code to send file path
WebElement elem = driver.findElement(By.xpath(“//*[@id=’patent-upload-container’]/span/input”));
elem.click();   // working fine but commented as i need to send file location
elem.sendKeys(“C:/driver/test.exe”);     //not working
 
I tried using different xpath, css, by name. Its not helping. No error is coming, but file is not getting uploaded.
Note : This webelement is behaving strange. I checked visibility of element using isDisplayed(), its false and still its getting clicked.
  • I tried setting visibility to true using java script executor, it did not helped.
  • Used Actions class to focus element and then sendKeys(), its also not working .
  • Used thread.sleep(0 and expected condition to wait for visibility of element but nothing is helping. In html attached, i did not find any attribute set to hidden
2.autoIT: As an alternative, i clicked element that opened window dialog box. I wrote autoIt script and that is working perfect;y fine. 
Issue is : I need to give generic path. I have to run my test on windows and lynx both. So we are having central repository codes where file is stored.
If i use : ControlSetText(“Open”,””,”[CLASS:Edit; INSTANCE:1]”,”M:\codes\ipd\apps\evsd_documents\tests\Glucose_sensors_importable_test10.xlsx“) it works fine. 
If i use : ControlSetText(“Open”,””,”[CLASS:Edit; INSTANCE:1]”,”..\codes\ipd\apps\evsd_documents\tests\Glucose_sensors_importable_test10.xlsx“) it gives error.
Please help me if anyone has faced this scenario.
 
Here is html code of select file element :

 
 
Thanks,
Garvita Girotra

Hi Garvita,

 

Try with that

driver.findElement(by.xpath(“//input[@name=’file_field]”)).sendkeys(“Your path to that file”)

make sure the file is from .xls or from those given formats

 

here you have added this line

elem.sendKeys(“C:/driver/test.exe”);     //not working

the file you are passing here is .exe

that could be the issue that you are not able to upload ur file.

 

you can share your exception you are getting if this workaround dont work for you

 

 

 

on July 12, 2017.

Hi Ankit,

That i added just like that. I am sending :

elem.sendKeys(“M:\\codes\\ipd\\apps\\evsd_documents\\tests\\Glucose_sensors_importable_test10.xlsx”);

 

Thanks,

Garvita Girotra

on July 12, 2017.

are you able to upload file manually?? can i see the exception you are getting in file upload??  

The html seems pretty simple i am sure you must be missing some basic thing

 

I guess u need to comment this line

// elem.click();  

and then try uploading without clicking on element

on July 13, 2017.
Add Comment
  • 1 Answer(s)

    Hi Garvita,

    you Can also use java awt Robot to upload a file using selenium

    Please find the below code

     

    StringSeclectio sel = new StringSelection(path of the file)

    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sel, null);

    Robot robot = new Robot();

    // Release Enter
    robot.keyRelease(KeyEvent.VK_ENTER);

    //Press CTRL+V
    robot.keyPress(KeyEvent.VK_CONTROL);

    robot.keyPress(KeyEvent.VK_V);

    // Release CTRL+V
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_V);
    Thread.sleep(1000);

    // Press Enter
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    robot.keyPress(KeyEvent.VK_ENTER);

     

    This can be useful please try it once

    Default Answered on July 12, 2017.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.