0

Not sure how to upload file using sendkeys.Can someone provide some sort of sample script which will upload image in this URL "http://elance.wetwaresoft.com/account/register"

SO that i can refer that.

4
  • If sendKeys is not working, then you can also use robot class. Just set the path to a variable and add it to system clipboard. then using robot send ctrl+v followed by enter. Commented Feb 18, 2015 at 19:16
  • Vivek i wanted to do it via Sendkeys.Please check below link for the code. Please comment on it and tell what i did wrong(Gave comment Access) docs.google.com/document/d/… Commented Feb 20, 2015 at 13:05
  • I have provided some robot class code to interact. Do check it. In case of sendKeys...i will try to work it out...though certain events are attached..not sure will that work. Commented Feb 20, 2015 at 19:38
  • Thanks i will check that... Commented Feb 20, 2015 at 19:39

3 Answers 3

1

You should be using SendKeys() to accomplish this. I provided a sample code block for you to see how this can be done.

By byCss = By.CssSelector("[id='ProfilePic'][type='file']");
String filePath = "my\\file\\with.extension";
IWebElement element = new WebDriverWait(_driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists(byCss));
element.SendKeys(filePath);

Note: Written in C# and would be fairly easy if you are using Java

Sign up to request clarification or add additional context in comments.

Comments

0

You can do this first click upload button by selenium and then call the below mentioned function and passing it absolute path in string

public void uplaodFile(String path) throws AWTException, InterruptedException
{
    Thread.sleep(5000);
    Robot rb=new Robot();

    //Get file path
    StringSelection stringSelection = new StringSelection(path);
    //Copy Path on Clipboard
    Toolkit.getDefaultToolkit().getSystemClipboard()
    .setContents(stringSelection, null);
    //Paste Clipboard Data
    rb.keyPress(KeyEvent.VK_CONTROL);
    rb.keyPress(KeyEvent.VK_V);
    rb.keyRelease(KeyEvent.VK_V);
    rb.keyRelease(KeyEvent.VK_CONTROL);
    rb.keyPress(KeyEvent.VK_ENTER);
      rb.keyRelease(KeyEvent.VK_ENTER); 

}

Please make sure these imports too

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

Comments

0

Selenium will not control windows based elements. So, For handling this file upload scenarios, I suggest to go for AutoIT. It is a keyystroke simulator for windows OS.

If you are new to AutoIT, use this link to get a basic understanding.

AutoIT Download Link :- https://www.autoitscript.com/site/autoit/downloads/

AutoIT Tutorial Link :- https://www.autoitscript.com/autoit3/docs/tutorials/notepad/notepad.htm

OK. Now assuming you know how AutoIT works, follow the below mentioned steps to create autoIT file for file uplaod, which will upload image using firefox browser on a Windows OS.

1) use the below script in AutoIT editor and save the file in name "ImageUpload.au3" and save it in your project root folder.

WinWait("[Title:File Upload]", "", 130)

WinActivate("[Title:File Upload]")

Sleep(2000)

$deskpath = "E:\image.jpg"

Send($deskpath);

Sleep(2000)

Send("{TAB}")

Send("{TAB}")

Sleep(2000)

Send("{ENTER}")

2) Compile the script (Tools>Compile) and it will generate "ImageUpload.exe" file for you.

3) Now we have to call and execute the autoIT file after you click on File upload button. That code will look something similar like the one mentioned below.

/* your JAVA code here */

// This will click on Select Image button

driver.findelement(By.id("ProfilePic")).click();

// Place your autoit file in the project folder

Runtime.getRuntime().exec(".\ImageUpload.exe");

4) That's it :) Let me know if need any further help on this.

(Do not forget to up vote if this answer helps you)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.