0

I want to use Java Selenium to upload the file in the following input element(upload element).

<input type="hidden" ng-model="model[options.key || index]"      
 id="formly_21_fileupload_args_content_substrate_surface_media_documentURL_0"
 name="formly_21_fileupload_args_content_substrate_surface_media_documentURL_0"
 formly-custom-validation="options.validators"
 class="ng-pristine ng-untouched ng-invalid ng-invalid-wizard-validation">  

I have tried to use 'sendKeys' but got the error informatoion:

upload.sendKeys("filePath"); // element is invisible

then I tried to use JavascriptExecutor in the script to change the visibility of the Element (type="file"), but still got the same error: element is invisible:

JavascriptExecutor jsexec = (JavascriptExecutor) driver; 
jsexec.executeScript("arguments[0].type='file'", upload);  

I also tried to use the parent element 'div' to do the upload, but got the error "cannot focus the element".

is there anyone can help me to solve this problem?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Jialei Cheng
  • 141
  • 1
  • 2
  • 8

2 Answers2

0

As per your question and the HTML you have shared the <input> tag is having the attribute type="hidden". To invoke sendKeys() on the <input> tag you can use the following solution:

WebElement uploadElement = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@class='ng-pristine ng-untouched ng-invalid ng-invalid-wizard-validation'][contains(@name,'_fileupload_args_content_substrate_surface_media_documentURL_')]")));
String jse = "arguments[0].type='file'";
((JavascriptExecutor)driver).executeScript(jse, uploadElement);
uploadElement.sendKeys("absolute_path_of_the_file_to_be_uploaded");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

What I'm guessing your issue to essentially be, is that there is probably a wrapper object to beautiful the default input element. It is common for web developers to 'hide' this default input element and as such, Selenium will fail to click on this object because its display attribute is set to 'none'.

We may need to look at the other HTML tags within that DOM hierarchy, but I suspect there is probably another input tag nested 1 or 2 levels below, that looks something like this. It should at least have a display attribute set to none:

<input display: none;> </input>

To upload a file, you will need to sendKeys directly to this element instead. But first, you will need to make it visible:

//the 'upload' variable here refers to the input element with display: none;

JavascriptExecutor jsexec = (JavascriptExecutor) driver; 
//First, change the display to inline to expose the underlying input element
jsexec.executeScript("arguments[0].display='inline;', upload);

After the above code is executed you should see a raw input element appearing. This is the element you would want to sendKeys to:

//After that you can go ahead to upload the file:
upload.sendKeys("path of the file");

In all honesty, without seeing the entire DOM, I am not able to advise whether the below line is necessary. You will need to try and see for yourself:

jsexec.executeScript("arguments[0].type='file'", upload);
  • If no such nested input element exists, just try the code above with the current input element you found.
Timothy T.
  • 1,031
  • 1
  • 12
  • 25