0

i use C# Selenium in my App, i click on file input and (open window) file dialog show up. I need to pass filepath and then hit enter (return) key.

Requirement: work even if current user is signed out, desktop is locked, RDP session closed

  • Selenium built-in SendKeys did not work at all.
  • Windows.Forms.SendKeys.SendWait() work not so excatly, path submit is ok, in order to hit enter, process wait for user session. Nuget -
  • InputSimulator / WinApi - UAC (UIPI) access denied

Any possibility to submit file without opening dialog in javascript ?

Thanks

Example using Selenium

var button = web.FindElementByXPath("//span[@class='upload']");
button.Click();

Not working

button.SendKeys("path"); //not input element
Windows.Forms.SendKeys.SendWait("..."); //not working for closed user session
InputSimulator.SimulateTextEntry("path"); //access denied because of Windows UIPI
apincik
  • 341
  • 4
  • 14
  • Possible duplicate of [Selenium WebDriver and browsers select file dialog](https://stackoverflow.com/questions/8851051/selenium-webdriver-and-browsers-select-file-dialog) – JeffC Oct 10 '18 at 22:26
  • Not when clickable element which triggers file dialog opening is not file input, does not resolve question. – apincik Oct 12 '18 at 20:31
  • You haven't explained enough in your question to know what you are referring to. You need to add details like what you've tried (add your code), what the result was (including any error messages), and the relevant HTML (properly formatted). *Then* we can better judge whether this is a dup or not. Part of proving that it's not a dup is you showing the code that comes from that other question and explaining how it didn't work, etc. – JeffC Oct 12 '18 at 22:17
  • I wrote use-cases, solved finally anyway. thanks (downvoted :/) – apincik Oct 12 '18 at 22:47

2 Answers2

0

In case explained above, there was no HTML input of file type (see comments). Open file dialog popup after click on other HTML element. Passing any string to that element had no success.

The best and probably the easiest solution which i used in c# application was using AutoIT.

Install-Package AutoItX.Dotnet

Call after click to open dialog

AutoItX.ControlSend("Open", "", "", _sysPathToTheFile);
AutoItX.ControlSend("Open", "", "", "{ENTER}");

Optionally, you can call

WinActivate(), WinWaitActive()...

But ControlSend() works well also when window is in background or in my case, use session is closed, locked screen and similiar situations.

apincik
  • 341
  • 4
  • 14
0

If there is a file input (<input type='file'/>) somewhere in the dom, you can do something like this:

https://www.selenium.dev/documentation/webdriver/remote_webdriver/#local-file-detector

IWebElement upload = driver.FindElement(/*select the <input> element*/);
upload.SendKeys(filepath); // as string
Darkproduct
  • 1,062
  • 13
  • 28