BelindaH
BelindaH

Reputation: 11

Selenium C# -loading text into text box works in IDE but not in Webdriver

I'm trying to load an image into a browser using Selenium Webdriver & C#. When I use the Selenium IDE it works fine, the IDE simply enters the file path into the text box and the image loads. However when I import the same code into Visual Studio and run it via Webdriver I get an "InvalidOperationException:Element must be user-editable in order to clear it".

I have a suspicion that this is related to the instance of the browser that is being used. When I run the test via the IDE it uses an already open instance of Firefox, when I run via Webdriver it opens a new instance. Does this sound right? If so is there a work around?

If I click the textbox or the "Select" button to the right of the texbox it opens a file dialog box, but I loose control of the driver when I switch to the dialog box.

Otherwise is there a way I can load this image file?

A snippet of the code as uploaded from the IDE is below.
Thanks in advance for your help. Belinda

driver.FindElement(By.Id("Barcode_fileInput")).Clear(); driver.FindElement(By.Id("Barcode_fileInput")).SendKeys("C:\Users\Belinda\Pictures\ValidImageType.jpg");

Upvotes: 1

Views: 654

Answers (2)

Saifur
Saifur

Reputation: 16201

<span class="btn btn-success btn-file">
  <i class="fa fa-plus"></i>
  Upload File
  <input type="file" multiple="" name="files[]">
</span>

If your html looks something like above use the selector pointing to input tag.

Driver.FindElement(By.Name("files[]")).SendKeys("C:\Users\Belinda\Pictures\ValidImageType.jpg");

this should upload the file if your site does not restrict it. I am using C#

Upvotes: 0

Major
Major

Reputation: 455

Just use driver.FindElement(By.Id("Barcode_fileInput")).SendKeys("C:\Users\Belinda\Pictures\ValidImageType.jpg");

Don't include driver.FindElement(By.Id("Barcode_fileInput")).Clear();

Upvotes: 0

Related Questions