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

182 views
Skip to first unread message

garvitag...@gmail.com

unread,
Jul 11, 2017, 5:08:08 AM7/11/17
to Selenium Users
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
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.

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

Xiang Dong

unread,
Jul 11, 2017, 5:14:11 AM7/11/17
to seleniu...@googlegroups.com

remove elem.click();   and try again, no need to get the file dialog popup. Selenium is not able to handle it.


--david




From: seleniu...@googlegroups.com <seleniu...@googlegroups.com> on behalf of garvitag...@gmail.com <garvitag...@gmail.com>
Sent: Tuesday, July 11, 2017 5:08 PM
To: Selenium Users
Subject: [selenium-users] Not able to upload file using sendKeys()- Please help
 



Thanks,
Garvita Girotra

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/ba2dcbd8-d76c-482c-8b47-4749df194a5e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

garvitag...@gmail.com

unread,
Jul 11, 2017, 5:44:31 AM7/11/17
to Selenium Users
Hi David,

Its already commented. Still, i deleted it. I am not getting results. 
I tried sending "Hello" except whole file path just to get idea even if its executing. I got error, not absolute path.

Thanks,
Garvita
To post to this group, send email to seleni...@googlegroups.com.

Xiang Dong

unread,
Jul 12, 2017, 2:47:18 AM7/12/17
to seleniu...@googlegroups.com

Your input seems only accept .xls, .xlsx, could you send a right file to it? the exe file will be rejected.


Best Regards,

--david




Sent: Tuesday, July 11, 2017 5:44 PM
To: Selenium Users
Subject: Re: [selenium-users] Not able to upload file using sendKeys()- Please help
 

sam khan

unread,
Jul 20, 2017, 10:37:48 PM7/20/17
to Selenium Users
Hi Garvita

exe file will be rejected possibily you can use ROBOT class, just given below hope it will work.



package twit;



import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;


public class UploadImg {

public static void main(String args[]) throws InterruptedException, AWTException{
   
   
    WebDriver driver = new FirefoxDriver();
    {
    driver.get("http://imgur.com/upload");
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.findElement(By.xpath("/html/body/div[5]/div/span[1]/div/div/div[2]/div[2]/label")).click();
    driver.findElement(By.xpath("/html/body/div[5]/div/span[1]/div/div/div[2]/div[1]")).sendKeys("/home/Downloads/bc89ed638d1192c8f082f47806dfaf9c.jpg");
    Robot robot = new Robot();
    Thread.sleep(1000);
 


    
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_V);
    Thread.sleep(1000);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTE
R);
    
    }
    
    
    
    
}
}

pranab singh

unread,
Jul 21, 2017, 7:25:07 AM7/21/17
to seleniu...@googlegroups.com
Hi Garvita,


Please ensure that the xpath/css/etc is clicking the text box where you need to provide the path of the file to be uploaded.
Try the below , hope it would work fine.Its same as how sam khan has given the solution.


                setClipboardData("Provide the path of the file to be uploaded with extension");
                                              eg-"C:\Users\ISILDT10015\Downloads\GBS\Software\build.xml" or file.xls etc
driver.switchTo();
Thread.sleep(3000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(2500);
public static void setClipboardData(String string) {
  StringSelection stringSelection = new StringSelection(string);
  Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);




Thanks,
Pranab Chetri

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/1272bd75-eac0-4370-9546-9b6631f7faf7%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages