0

Page contains this form:

<form target="PID297_TGT_FRAME" action="/app/upload/57aa897a64d9" class="form form-default" method="post" enctype="multipart/form-data">
    <div>
        <input type="hidden">
        <input name="PID297_file" class="file-upload" type="file">
        <div aria-pressed="false" role="button" class="v-button" tabindex="0">
            <span class="v-button-wrap">
                <span class="v-button-caption">Import</span>
            </span>
        </div>
    </div>
</form>

Now I want to upload file to form. After searching stackoverflow I found that it is possible to send file path to input with type file. So I did this:

var elem = Driver.FindElement(By.Name("PID297_file")).SendKeys(filePath);

Unfortunelly I am getting Exception with Message:

Element is not currently visible and so may not be interacted with

Is there something wrong with my code?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
dafie
  • 951
  • 7
  • 25

2 Answers2

1

The element needs to be visible for selenium to access it. If there is some other action that enables the file upload, do it first in selenium code.

Deepak Garud
  • 977
  • 10
  • 18
  • Adding `Driver.RunScript("jQuery(\"input\").attr(\"visibility\", \"visible\");");` did nothing. Still element is not visible. – dafie Oct 11 '18 at 11:46
  • Is the Input box visible to the user ? Is it hidden because of some class? See this question also - https://stackoverflow.com/questions/21685415/upload-file-to-hidden-input-using-protractor-and-selenium?noredirect=1&lq=1 – Deepak Garud Oct 11 '18 at 11:59
0

This error message...

Element is not currently visible and so may not be interacted with

...implies that the WebDriver instance was unable to locate the element as it was not visible.

The positive take away from this message is the desired element is present within the HTML DOM but not visible possibly as it is not within the Viewport

Solution

You need to bring the desired element within the Viewport before invoking SendKeys() as follows:

  • CssSelector:

    new WebDriverWait(Driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.file-upload[name$='_file']"))).SendKeys(filePath);
    
  • XPath:

    new WebDriverWait(Driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@class='file-upload' and contains(@name,'_file')]"))).SendKeys(filePath);
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Visible is not determined by whether the element is in the viewport. You can look at the source and see for yourself. – JeffC Oct 11 '18 at 15:36