0

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

Picture of Windows Explorer:

pic of Windows Explorer

How can I do that?

What I tried is:

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.

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
GreenThus
  • 1
  • 1
  • 4
    WebDriver, as the name says, is for driving web browsers. Not desktop applications. What are you trying to do and why do you think you need Selenium to do it? If you want to open a text file with the default text editor, `Process.Start(pathToFile);` is probably enough – Panagiotis Kanavos Jul 01 '22 at 11:54
  • 1
    `Now I would like to send c:\temp\file1.txt to it.` what is that supposed to do? Are you trying to select an existing file for the user? Copy a file to that folder? You can manipulate files with C# directly. To open Windows Explorer with a file already highlighted you need to actually execute `explorer.exe` with the `/select` argument, eg `$"/select, \"{filePath}\""` – Panagiotis Kanavos Jul 01 '22 at 11:58
  • If you want to select a file, check [Opening a folder in explorer and selecting a file](https://stackoverflow.com/questions/334630/opening-a-folder-in-explorer-and-selecting-a-file) – Panagiotis Kanavos Jul 01 '22 at 12:03
  • welcome to stackoverflow greenthus. i do curious with your question, are you perhaps trying to upload file using WebDriver? – Bagus Tesa Jul 01 '22 at 12:17

2 Answers2

0

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 + "\"");
Jackdaw
  • 7,626
  • 5
  • 15
  • 33
GreenThus
  • 1
  • 1
  • Add what you really want in the question itself. I already added in a comment how to do this and linked to a duplicate. The argument is `/select, filepath`. The path will have to be enclosed in double-quotes if it contains spaces. – Panagiotis Kanavos Jul 01 '22 at 12:32
  • If you want to add more information add it in the question itself. Not as an answer. What does `still not mission accomplished.` mean? Don't ask people to guess what you want to do – Panagiotis Kanavos Jul 01 '22 at 13:01
0

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.

GreenThus
  • 1
  • 1