SendKeys to a windows file dialog - c#

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.

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

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']"...

Related

Using .NET: How Can I Edit an HTML Form Input Text box?

I want to use .NET to fill a input text box, that's inside an HTML form inside an IE window.
This HTML form is already open inside an Internet Explorer window. So my program doesn't need to open IE, nor does my program need to open an HTML page because, the HTML page is already open.
Can someone point me in the right direction please - how do I edit an input text box inside an IE window?
Thanks for the help guys!
hardcore version
Look at this thread Simulating Key Press c#
I assume you know layout of the form:
write function that will invoke keypress events InvokeKey, take care of tab and space keys as well
Write a function that will convert string to series of keypress events let's say WriteItForMe(string input) using function in 1.
To fill form do this (example)
WriteItForMe("Michael");
InvokeKey("#tab");
WriteItForMe("Scofield");
InvokeKey("#tab");
etc.

Read content from a URL, place into a textbox

I'm trying to read lines from a website and then, copy it into my textBox2.
textBox1 will have a website's URL like http://example.com.
When I click on button1 I'd like to read HTML content from the above URL, and please that info textBox2.
Should I use HtmlAgilityPack?
How can this be done?
2 =================================================
So, for example, i copy this link into my "textBox1"
http://www.mineshaftersquared.com/server/DareCraft#
So, is there a way to make app copy everything from:
//*[#id="Plugins"]
to
//*[#id="rightInfo"]/section[2]/div/div[1]/table[2]
/this is XPath
/It don't have to in XPath, but this was the only way I could show.
You don't install HtmlAgilityPack, you reference if from your project.
Read about WebClient class (OpenRead method), you probably want to use it to get the pages.
Here is a tutorial you might want to start with:
http://www.codeproject.com/Articles/33798/HTTP-GET-with-NET-WebClient

how can I upload a file via file input box using webbrowser in C#

May be this question is asked in past but I have searched and not found its solution yet.
I have tried all the options that I have found till now but all in vain.
SendKeys doesn't work as it does not fill the file input box with file path, that is to be uploaded.
Cannot set file input box "SetAttribute" value as there is no value attribute available:
thats all.
If I use element.focus() it pops up "choose file to upload" dialog and now I don't know how to fill it programmatically and open it in file input box.
I want it to be automated completed so that user does not have to interact with the application.
Application shall pick the file from hard disk from given file path and fill other fields of form then start uploading, all using webbrowser control in windows form application.
No solutions found!
Can anyone help please? (This is my first ever question on stackoverflow, therefore if I am doing anything wrong then please guide, I mean if I am not allowed to post such question!)
Here is the code:
HtmlElementCollection heCollection = doc.GetElementsByTagName("input");
foreach (HtmlElement heSpan in heCollection)
{
string strType = heSpan.GetAttribute("type");
string strName = heSpan.GetAttribute("name");
if (strType.Equals("file") && strName.Equals("file"))
{
heSpan.Focus();
//heSpan.SetAttribute("value", "test.jpg");
SendKeys.Send("C:\\1.txt");
//heSpan.InnerText = "c:\\1.txt";
}
//Title for the attachment
if (strName.Equals("field_title"))
{
heSpan.InnerText = "1.txt";
}
}
When this code executes, cursor starts blinking in fine input box (as I have set heSpan.focus()) but the file path doesn't show in the file input box.
If I implement
heSpan.InvokeMember("click");
It opens the choose a file to upload dialoge/popup window and there I get stuck, because I don't know how to fill that popup dynamically and then insert the file path in file input box.
Try setting the focus to the WebBrowser control right before you set the focus to the input field.
That worked for me.

<input> Cant fire the input event programmatically

Hi all - I need the user to select a file from a Dialog. I then only need the path to that file. I've been looking and the only way it seems is to use the <input> but i can't have the user click the choose file button, it must be "clicked" programmatically
Any help would be useful.
PS. THIS IS MY SECOND DAY OF ASP>NET - i also dont know javascript
Aiden
You can use jQuery to apply an event handler to the change event for the input type="file", but some browsers will return the file path name to the file, some will display a dummy path (with correct file name), and others will just give the file name with no path.
EDIT:
To click the file dialog programatically, you can hide it with css using display: none, and then do $("#<%=myFileInput.ClientID%>").click(); to prompt the dialog.

pre-visualize an image

I'd like to pre-visualize an image in a image box before save it in a directory.
How can i do this, i use a checkbox to see if the user wants to pre-visualize or not because i dont find another way to do this without a checkbox.
I use file upload to upload the image.
string serverFileName = "";
serverFileName = Path.GetFileName(Upload.PostedFile.FileName);
Upload.PostedFile.SaveAs(MapPath("~/fotosPerfil/") + serverFileName);
i use this piece of code to save the image.
I think this post is exactly what you need to implement. Check out Ivan's solution.
Yes, indeed you can read the path selected by the user and display the image in an <img> tag, all client-side prior to uploading.

Categories