0

I tried using selenium for upload file to website. Site is using "DropzoneJS", witch delete input type="file" and replace it.

Sourse html code:

<input type="file" id="input-file" name="input-file" value="file">

After loaded page sourse html replaced:

<div class="dz-message needsclick">
<div class="dropzone-image"></div>
<p class="dropzone-text">Drag and droup you file here <span>or</span></p>
<button class="cta low dropzone-upload-btn" type="button">Upload file</button>
</div>

I tried to set sourse file value:

String script = "document.getElementById('input-file').value='" + "D:\\\\temp\\\\file.txt" + "';";
((IJavaScriptExecutor)driver).ExecuteScript(script);

But of course got a error (because input tag with id="input-file" replaced):

OpenQA.Selenium.WebDriverException: 'javascript error: Cannot set property 'value' of null

How can I upload file?

Updated:

I found answer:

If input with type=file is hidden, needs find it. Follow code helped me:

var select = Driver.FindElement(By.CssSelector("input[type=file]"));
select.SendKeys("D:\\\\temp\\\\file.txt);
Dmitry
  • 327
  • 1
  • 11
  • Can you try the answer here - https://stackoverflow.com/questions/61551055/how-to-handle-windows-file-upload-in-net-core-using-selenium/61566075#61566075. do you have a link to the site you are working on? – Dazed May 20 '20 at 14:35
  • I tried like it: driver.FindElement(By.Id("input-file")).SendKeys(filePath); but it doesn't work – Dmitry May 20 '20 at 19:38
  • make sure your file path looks like: string filePath = @"D:\temp\file.txt"; – Dazed May 20 '20 at 19:54
  • i added if (!File.Exists(filePath)) return; but the error remained – Dmitry May 20 '20 at 20:22

1 Answers1

0

I have written this method based on the answer given above. (Thanks for the assist @Dmitry)

  public void DropZoneAttachFile(int dropZoneListNumber, string imagePath)
    {
        //dropZoneListNumber : input[type=file] will return multiple DZ elements if there are more than 1 DZ on a page.
        //Figure out what position in the list the DZ you want is and pass the int to this method along with a local image path
        IList<IWebElement> dropZoneElement = driver.FindElements((By.CssSelector("input[type=file]")));
        dropZoneElement[dropZoneListNumber].SendKeys(imagePath);
    }
agleno
  • 388
  • 4
  • 17