Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.
<span class="span3 config_camera_wrapper img-rounded" style="border: 2px solid #fff;" align="center">
<img class="img-rounded" src="images/bookappt_v3/logo-placeholder.png" alt="140x140">
<i class="config_camera img-circle icon-camera icon-2x" onclick="$('#cropImageInput').trigger('click');"></i>.

Above is the code for upload image. Check the link to see the how the image upload option looks http://dropmocks.com/mEbKUV.

I tried the below code to upload image.

WebElement upload = driver.findElement(By.xpath(".//*[@id='profile_configure_company_details']/div/div[2]/div/ul/li[3]/span[2]/i"));
                upload.sendKeys("/Users/sathiyarengarajan/Desktop/images(1).jpeg");
                driver.findElement(By.xpath(".//*[@id='cropandsave']")).click();

But it doesnot works.

share|improve this question

1 Answer 1

As far as I know file uploads always need a <input type=file> tag in the HTML. Your example HTML does not contain any. Often its hidden somewhere in the page, so that input element is not shown on the page. It could also be generated as you the onclick event fires. The example page you linked on dropmocks has no upload functionality, it does nothing (or I can't figure out how it should work).

Once you figured out where the <input> is located you can use your sendKeys() code on that element instead and it should work. Also see http://stackoverflow.com/questions/3300580/webdriver-file-upload for more methods to upload a file with Selenium

I would ask advice of your development team, they surely can assist you to find the correct <input> tag and help making this test work.

For reference you look at this example/test styled file uploader or this question about file uploads without inputs.

share|improve this answer
    
I figured out the code for image upload.<span id="cropImageSpan"> <input id="cropImageInput" class="hide" type="file" name="Image">. Below is the code I tried. driver.findElement(By.id("cropImageInput ")).sendKeys("/Users/sathiyarengarajan/Desktop/bouquet1.jpg");. Browse file popup opens and testcase fails –  STE May 8 at 12:07
    
Guess the sendKeys is triggering the OS file-dialog. You should try the JavaScript way to inject the filename, see the accepted answer on stackoverflow.com/questions/3300580/webdriver-file-upload –  Niels van Reijmersdal May 8 at 17:36

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.