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.
Related
New to windows forms so please bear with me.
I have a simple application that displays a few buttons, and I'd like the attributes of these buttons to be set by the user using some kind of external config file e.g. config.ini.
In the config file, the attributes would be set out something like this:
[Button 1] //button identifier
isEnabled = True //show/hide the button
colour = "#FFFF" //button background colour
caption = "Button" //text to display on button
action = "Action" //action when button is clicked
The config file should be stored with the program executable, and when the program is launched, it will display the buttons and text defined in this file.
The action determines what happens when the button is clicked. This could either be opening a new page, or writing a string to a variable. The aim of the program is to allow users to navigate through a couple of menus, and then select a button which will print out the string.
Edit: if the action is to go to a new page, it will just say 'Next Page' so I guess I can just use an if/else for this. I.E.
if(string.Equals("Next Page"){
//go to next page
} else{
//save string to variable
}
I know how to set button attributes in the code, but I don't understand how I can read the config file. Its important that these attributes can be set from the external file so that the program can be easily configured without needing to rebuild it.
I just try to write correct path to click exactly this button what I want on my page. I will give you an example page for testing. You just have to download the HTML file and open it in your browser.Code of HTML page
What we now want to do?
If you run this HTML file you will see all page.
And now we want to make a Click on exactly this button on screen :
After when you click on this button you will see click counter below: like this :
Have anyone idea how to click it? I tried few ways and can't find solution still. Please help.
I try at least :
drive.FindElement(By.XPath("//tr[class='ng-scope']/td[text()='Wylaczenie nadan RDF'] and button[#title='Skip']")).Click();
and
drive.FindElement(By.XPath("//tr[text()='Wylaczenie nadan RDF']/button[#title='Skip']")).Click();
+ more and more and can't just write fine this Path to click exacly this button.
And unique value is <td class="ng-binding">Wylaczenie nadan RDF</td> - i prefer to get path by this.
You need to use below XPATH for the same
//tr[td='Wylaczenie nadan RDF']//button[#title='Skip']"
//tr[td='Wylaczenie nadan RDF'] will take you to the row which contains that text and then you use //button[#title='Skip'] to find that button
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']"...
I am using a C# WebBrowser to navigate to a page.
After navigate to that page I extract some link from its source code and then start to navigate to those sub_links in a loop which I get from its parent url.
On every sub_links there is a link which is look like:
<a onclick="sysMT('SYSext.Sup_Filetype','type','SYSext.Sup_Filename','Filename.exe','SYSext.Sup_Referer','http://abc.com');"
id="ctl00_UCDownloadFile1_Accept"
href="javascript:__doPostBack('ctl00$UCDownloadFile1$Accept','')">Accept</a>
the id of a button is "ctl00_UCDownloadFile1_Accept" is same in all its sub_links.
When I click on above link manually a file start to download.
I want to "click" on above button using c# code.
I am able to click on above button using this code on webBrowser1_DocumentCompleted event:
HtmlElement elementById = webBrowser1.Document.GetElementById("ctl00_UCDownloadFile1_Accept");
if (elementById != null)
{
elementById.InvokeMember("click");
Application.DoEvents();
}
But a script error message box appear saying
"An error has occured in the script on this page.
Error: The value of the property 'sysMT' is null or undefined, not a Function object.
Do you want to continue running script on this page?
Yes NO"
Not able to understand what I am missing in this. Did Parent url make some cookie which helps to download the file? Don't know how to download that file and also download file link.
I am new to C# WebBrowser.
Update
I am able to remove a script error message box using
webBrowser2.ScriptErrorsSuppressed = true;
But not able to download all file which are their in sub links i.e.
If I get 3 links from a parent url, then I navigate to first link, click on Accept link and then a box appear asking for a action to RUN SAVE CANCEL.
But I get this box only for third (last) link.
How to overcome with this problem, I want to download all file from each links without any prompt which asking for RUN SAVE CANCEL. Directly SAVE a file.
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.