4

I want to send the string ABC to the input field of a windows file dialog. With this code line I can set the focus to the correct element. I see a blinken cursor.

var filedialogOverlay = drv.SwitchTo().ActiveElement();

But the following code doesn't write the string into the element.

Thread.Sleep(1000);
filedialogOverlay.SendKeys("ABC");

EDIT: The file upload prompt is shown by a website which I want to test. Because of black box testing I can't see the source code. Is there a tool to analyse the GUI?

When I right click the input element I get the following choices.

enter image description here

kame
  • 20,848
  • 33
  • 104
  • 159
  • If you mean File Upload Prompt like http://www.cumc.columbia.edu/it/howto/remote/img/dnuploadfile25.jpg, your code will not work. `drv.SwitchTo().ActiveElement();` allows to switch to element which you currently focused on, e.g. text input field with cursor inside... To upload file you need to send path to file to `` element – Andersson Nov 30 '16 at 15:53
  • 1
    The method `ActiveElement()` returns the active element in the page and not the file dialog. Selenium doesn't support the file dialog but it supports file upload by calling `Sendkeys` directly on the `` element with the path of the file. – Florent B. Nov 30 '16 at 16:32
  • 1
    @Florent B. Could you show me the full code in an answer please? – kame Dec 01 '16 at 09:04

2 Answers2

3

You can use the SendKeys.SendWait of Windows Form

//Input the file path into the filename field:
SendKeys.SendWait(longfilepath);
//Input "Enter" key
SendKeys.SendWait(@"{Enter}");

https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.sendwait(v=vs.110).aspx

Viet Pham
  • 214
  • 2
  • 5
  • Sorry for late reply, you can refer to this link : [link](http://stackoverflow.com/questions/15653921/get-current-folder-path) – Viet Pham Dec 04 '16 at 15:24
1

If you need to upload file, try to send path to file directly to appropriate input field:

drv.FindElement(By.XPath("//input[@type='file']")).SendKeys("ABC");

P.S. If there are more than one input fields for file upload located on page, you might need more specific XPath, like "//input[@id='some_specific_file_upload']"...

Andersson
  • 51,635
  • 17
  • 77
  • 129
  • But how do I get the input field name? I mean the standard dialog overlay from "Windows". – kame Nov 30 '16 at 16:02
  • To open file upload prompt you, obviously, need to click on button, right? Check `HTML` of that button and you should find mentioned `` element – Andersson Nov 30 '16 at 16:03
  • I have no chance to see the code. I am a black box tester. Is there a tool for analysing the GUI? – kame Nov 30 '16 at 16:12
  • You can check `HTML` simply with your browser. Use right click on required web-element and select something like `inspect element` – Andersson Nov 30 '16 at 16:14
  • Do not left click on Upload button to open File Upload Dialog. Right click on Upload button which is located on page – Andersson Nov 30 '16 at 16:25