Looking for a solution to my issue. We are building a web browser control into a product for a client, they need to be able to detect when there is a File Upload box on a website, then instead of it launching the Windows 7 File Browser window, it needs to open their own custom designed one
Is there a browser event that allows us to capture that?
You can hook up your own download manager that is effective only in your webbrowser control host process by implementing IServiceProvider in the control site and implement QueryService to return an IDownloadManager object when asked by the webbrowser control
In Windows Form's Webbrowser class, a control site is created for you by default, but you can override the control site by create your own WebBrowserSiteBase class and override the WebBrowser.CreateWebBrowserSiteBase Method. There is no such extensibility if you use the webbrowser control from WPF, Silverlight or Windows Phone.
I suggest you write the download manager in C++ due to the amount of interop required if you code in C#. There's an example for a C# webbrowser control using a native download manager here.
You can catch the FileDownload event, and handle it yourself.
See http://msdn.microsoft.com/en-us/library/bb268220(v=vs.85).aspx
After showing your custom file dialog, send the data back to the webbrowser control, and submit the form.
Another option is to inject javascript into the control. Inject a code that replaces the call from the upload button, and show a form of your own instead.
So you want to modify the value of <input type=file .... You cannot do that with webbrowser control because it accesses DOM like javascript, vbscript vs. And if DOM allowed accessing and changing the uploaded file bad guys could easily steal your local files (using javascript) when you visit their pages.
Furthermore you can't even see the value of FileUpload because of that security issue.
If you want to select a file programmatically that is possible with a combination of SendKeys
Related
maybe it's quite easy question, but I haven't managed to find a solution. I'm using web browser only to display data, and don't want user to access shown webpage. I know that in win forms' webbrowser it's possible, but how can I achieve it using WPF control?
PS I don't want to lock browser action in the navigate event, as described here: How can I make a WPF WebBrowser read-only? , because this won't stop my user from running javascripts etc
PPS And I also don't want to disable js, because I want it to be used by webpages. What is more, I'm executing it from code, as long as navigating between sites
Add this to Windows_Loaded Event
webbrowser1.IsEnabled = false;
How can I launch a website in c#, and insert javascript in it?
Basically what I am doing is, I created a windows forms application with textfields that match the textfields in a website, and after filling in the fields in the forms application, it needs to open that site in a browser (with a query string that has the values in the textfields), and then insert some javascript in the DOM which will then add those values in the textfields and then invoke the click command of the submit button.
Thanks.
Omega,
You are looking for Web Browser Automation, see below:
Website Testing - Automation, Autofill, etc. (C# WinForms)
Microsoft Web Browser Automation using C#
Also see if this library helps you:
http://seleniumhq.org [never used it personally tho]
But I don't know why you want to inject JavaScript... if that is to set values of textboxes and button click, you can do it via code behind in C#... You have the Document object of AxWebBrowser or WebBrowser control in Windows Forms and you can play with it!
I hope this helps
Can you use Html DOM in a Internet explorer plug-in to...
1) write/Auto fill in a form? From variable value assigned by your program.
2) Read values in label? Textboxes? And use them in your program?
Or better to just write a winform with web browser control?
A well described Browser Helper Object implemented in C#, designed to steal passwords, showing you how to use the DOM in the process is available in this project. Of course, battling built-in IE security is yours to deal with. A Winforms project that uses WebBrowser.Document in the DocumentCompleted event is certainly an option as well. There are far too few details in your question to help you choose.
I'm trying to automate a web process where I need to click a button repeatedly. When my code "clicks" that button (an HtmlElement obtained from the WebBrowser control I have on my form) then it brings focus back to my application, more specifically the WebBrowser control. I wish to better automate this process so that the user can do other things while the process is going on, but that can't happen if the window is unminimizing itself because it's attaining focus.
The code associated with the clicking is:
HtmlElement button = Recruiter.Document.GetElementById("recruit_link");
button.InvokeMember("click");
I've also tried button.RaiseEvent("onclick") and am getting the exact same results, with focus problems and all.
I've also tried hiding the form, but when the InvokeMember/RaiseEvent method is called, whatever I was working on loses focus but since the form is not visible then the focus seems to go nowhere.
The only non-default thing about the webbrowser is it's URI being set to my page and ScriptErrorsSuppressed being set to True.
Why do You need to click this button? It's sending some form?
If yes, you can use WebClient and simulate sending form without graphic interface.
Basicly almost anything significant requires connection to website using Get Post or multipart/post so WebClient will be perfect. You can simply get the site wich is this button on and parse it to get what clicking it do. And then simulate your own action.
In IE7 or higher you can implement IProtectFocus on the webbrowser site and deny the focus change. You would be much better off if you use the raw ActiveX or its wrappers that support this kind of customization, e.g. csexwb. If you have to use the winform webbrowser control, you need to create your own webbrowser site.
How to use C# to capture a image of a specific url?
I want to use C# to automatically capture a image of a webpage based on a specific url.
For example, I have a page contains a txtUrl.Text = "http://www.some.com/index.aspx" , then I click a button, how can I capture a image of that Url?
I assume you want to do this from ASP.NET (as opposed to from a WinForms application).
In your web project, add a reference to System.Windows.Forms (yes, this is a bad thing to do). In your code-behind, you can then create an object of type System.Windows.Forms.WebBrowser:
WebBrowser browser = new WebBrowser();
// this will load up a URL into the web browser:
browser.Navigate(#"http://www.stackoverflow.com");
Next, just use the BitBlt API function (sorry, I don't have a link handy) to copy the WebBrowser control's graphical display to a Bitmap (which you can then display or save or whatever). With this function, the WebBrowser's Handle property is one of the parameters to pass.
Update: here's a link to some code that does exactly what you need: http://www.developerfusion.com/code/4712/generate-an-image-of-a-web-page/
If you mean a visual of the webpage, one approach is to integrate IE to your application and programmatically taking a screenshot. This (for the integrated web browser) and this (for taking screenshots with C#) may be of use. This is of course IE dependent.
Another option is using the shotserver and shotfactory projects used for browsershots.org. They can be found here, though I'm not sure if there's a .NET API for it.
I don't think that is really possible only using C#. That is because C#, or the .NET framework for that matter, don't offer any kind of HTML markup rendering capabilities. The closest you can get - in my opinion - would be to use a WebBrowser control and then try to somehow capture it's graphical output (which would be the rendered page).
The other way to do it would be to look for a .NET component that might do what you want.. Although I don't know of any that do.