Home > Enterprise >  How can I sendKeys to the Windows Explorer with Selenium
How can I sendKeys to the Windows Explorer with Selenium

Time:07-02

A link on the page opens the file explorer of Windows. Now I would like to send c:\temp\file1.txt to it.

pic of Windows Explorer

How can I do that?

What I tried was this:

string BaseWindow = _webDriver.CurrentWindowHandle; 

IJavaScriptExecutor jsExec = (IJavaScriptExecutor)_webDriver;
Thread.Sleep(1000);
jsExec.ExecuteScript("arguments[0].value = 'C:\\temp\\file1.txt'; ", BaseWindow);

which did not break the script actually but it didn't give the proper effect either.

CodePudding user response:

I got this:

System.Diagnostics.Process.Start("explorer.exe", "C:\test.csv"");

However that literally opens the Excel file. (which is also nice to know how to do that in automation by the way)

But I need to select a file in the Explorer (see 'pic of Windows Explorer')

I also tried this: System.Diagnostics.Process.Start("explorer.exe /select", "C:\test.csv"");

discovering this brought better results still not mission accomplished.

var FilePath = "'C:\\test.csv\'";
        System.Diagnostics.Process.Start("Explorer.exe", @"/select,"""   FilePath   "\"");

CodePudding user response:

Finally!! got it to work:

for all those people struggeling with the same thing... here is the solution:

you've got to switch to the active element then use sendkeys.

  • Related