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.
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.
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
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;
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)