0

What I'm trying to do here is upload an image file using sendKeys() in headless browser Firefox on a Linux machine (FYI: Tests are working fine in real browser FF:42, selenium 2.47.2)

WebElement element = driver.findElement(By.cssSelector("input[name='file']");
element.sendKeys("Path to file");

HTML code here:

<div class="form-group col-lg-8 col-md-8 col-sm-7" data-content="image-creative">
  <div class="form-group" data-content="image-creative-upload">
    <div class="upload-image-field">
      <label for="uploadImage">Upload an image in jpg, png or gif format</label>
      <div class="fileinput fileinput-new input-group" data-provides="fileinput">
        <div class="form-control" data-trigger="fileinput">
          <i class="glyphicon glyphicon-file fileinput-exists"></i>
          <span class="fileinput-filename"></span>
        </div>
        <span class="input-group-addon btn btn-default btn-file">
    <span class="fileinput-new">Select file</span>
        <span class="fileinput-exists">Change</span>
        <input id="uploadImage" class="file" type="file" placeholder="No image uploaded" accept="image/*" name="file">
        </span>
        <a class="input-group-addon btn btn-default fileinput-exists" data-dismiss="fileinput" href="#">Remove</a>
      </div>
      <p class="text-danger upload-error hidden">
    </div>
  </div>
</div>
zero298
  • 25,467
  • 10
  • 75
  • 100
Ranjith's
  • 4,508
  • 5
  • 24
  • 40

2 Answers2

1

You probably need to set the file detector if you are running a remote instance:

driver.setFileDetector(new LocalFileDetector());

WebElement element = driver.findElement(By.cssSelector("input[name='file']");
element.sendKeys("Path to file");
Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • But.. In my case, I'm using local driver instance to execute tests.. not using remote web driver instance (Also FYI: Jobs are running in AWS) – Ranjith's May 04 '16 at 12:50
  • To make my self clear.. I'm not running tests using selenium remote web driver using normal firefox driver in headless mode(xvfb). interestingly uploads are working in some other pages in the same application.. but only for this case, i'm unable to upload an image.. I have already attached page info in my question.. any help much appreciated!! – Ranjith's May 06 '16 at 13:46
  • If it's working in some other pages, then it's probably a timing issue. I would try to add a delay or wait for an element to be in the expected state before sending the keys. – Florent B. May 06 '16 at 18:51
  • I tried using wait time but still no luck.. one thing i observed in the process, when i verify element.isDisplayed() is returning me false and when i do element.isEnabled() giving me true.. is there something am i missing?? thanks – Ranjith's May 09 '16 at 12:34
  • Thanks @Florent B for your time.. I tried giving some waits in but not much luck though.. can only see spinner in screen shots :( – Ranjith's May 11 '16 at 10:47
  • There's nothing much I can do without a reproducible example. – Florent B. May 11 '16 at 10:50
0

The solution that worked for me is:

Updating to latest version of firefox in ubuntu machine is fixed the issue, Anyway thanks to @Florant B for your support.

Ranjith's
  • 4,508
  • 5
  • 24
  • 40