Submission of a webpage form using WebBrowser control in C# - c#

I have seen a lot of posts regarding this particular subject on SO as well as on the web in general and most if not all code is as seen below
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
webBrowser1.Navigate(new Uri("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onsubmit.htm"));
}
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
mshtml.HTMLDocument htmlDoc = null;
htmlDoc = (mshtml.HTMLDocument) this.webBrowser1.Document;
if (webBrowser1.Document != null)
{
foreach (mshtml.HTMLFormElement form in htmlDoc.forms)
{
form.submit();
break;
}
}
}
The code has no errors whatsoever but for the life its not submitting. The sample page that I am using has simple button, what it does, it alerts the selection of the radio button and then submits the form. For some strange reason when the form is submitted via code using the WebBrowser control, the form is submitted but the alert never shows up.
I am not sure what I am doing wrong here. Any help on this would be appreciated.

Would performing a click on the button do what you need it to do? You will need to add a COM reference to the Microsoft HTML Object Library (which you may already have). For example, if you load up google into the webbrowser control, this code will place "hello world" into the search box and perform the search:
mshtml.IHTMLDocument2 doc = ((mshtml.HTMLDocumentClass)webBrowser1.Document);
((mshtml.IHTMLElement)doc.all.item("q")).setAttribute("value", "hello world");
MessageBox.Show("Clicking I'm feeling lucky button");
((mshtml.HTMLInputElement)doc.all.item("btnI")).click();
Edit: I updated the code for the components that the WPF WebBrowser control uses. Also note that this sometimes throws a script error from google, but that appears to be a timing issue based on some of the ajax calls google has on the home page.

To fix your problem you need to replace line:
form.submit();
With following code:
var children = form as IEnumerable;
var inputs = children.OfType<mshtml.HTMLInputElement>();
var submitButton = inputs.First(i => i.type == "submit");
submitButton.click();
This will show alert about user selection and submit form.

I've got a more dirty one-liner working by injecting a JavaScript to submit the form
_webBrowser.InvokeScript("eval", new object[] { "document.getElementById('formName').submit()" });
That's been working for me when interacting with a site using a lot of JavaScript and button beyond the form.

Related

Adding Text Into A TextField But I have To Wait For The Pop Up To Open? C#

Scenario:
I have been able to click the button using c# and a webbrowser, but now I want to add text inside a text field after the pop up has opened.
Imagine the Events Tab on Facebook.
When you make an event it pops up a little box where you can enter text (AJAX).
I am trying to get the text from my text boxes into those text fields, after it has popped up and fully loaded.
I have tried using the async and await keywords with no luck, I have tried scanning the page but the pop up happens using AJAX so I can't rescan the page(I don't think) to grab the right div id to enter text in?
Any idea's?
Thanks
Here is my code so far:
private void web_postFB(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
if (btnClicked == false)
{
var links = web.Document.GetElementsByTagName("a");
foreach (HtmlElement link in links)
{
if (link.GetAttribute("className") == "mrs mlm _42ft _4jy0 _4jy3 _517h")
{
link.InvokeMember("click");
}
if (web.Document.GetElementById("u_f4_3") != null)
{
Here is where I want it to wait for the pop up to be fully opened. Then do the method below to insert the text into the text fields.
inputText();
}
}
}
}
This is most likely an adorned HTML popup, not a real window. You can try to process it with asynchronous polling of WebBrowser.Document.Body.InnerHtml for changes, using webBrowser.IsBusy, Task.Delay and async/await. A complete example can be found here.

change label text programmatically in c# web forms

I searched the internet over and over, and this seems to be very straightforward, but somehow it's not working.
I'm trying to create a web forms application in C#.
Button1 is supposed to download a lot of data from a website, and I want to Label1 to display "downloading" while the code in Button1 runs, and "done" after the code finishes, but Label1 is not changing at all after button click.
code is below:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "downloading";
//code for downloading data
Label1.Text = "done";
}
That's not going to work. That's all server-side code. The updated HTML is not sent back to the browser until after the Button1_Click handler is finished executing. So the browser will only see the "done" label text. Never the "downloading" text.
The easiest way to achieve your desired effect is to use client-side javascript (maybe using jquery) to update the label text to "downloading" before you submit the form. You can put your client-side javascript in the OnClientClick property of the asp.net button. The server-side code can then download the data and change the label text to done.
I don't know if this will still be helpful since the post is rather old, but I stumbled upon the question while searching for an answer to a question of my own.
Just use the Label1.Update() function after you change the label's text.
If you want to change text from downloading to done then you may do this:
Label lbl = (Label)this.FindControl("Label1");
if (lbl != null)
{
lbl.Text = "done";
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "downloading";
//code for downloading data
Label1.Text = "done";
Label1.Refresh;
}

C# Intercept Browse Button

Have a unique customer request which Im unsure how to tackle.
The customer has a webpage form with a browse button to select a file. When the browse button is clicked, instead of showing the local files, they want to pop-up a window with a textbox to enter a code. This code is then used to select a file from a local folder containing 1000 files each with their own code. They want to prevent the user from viewing the other files in that folder.
I did write a custom Windows form to mimic the webpage form but they already have the webpage online and would like to reuse it.
Any ideas how to intercept the browse button? I can use a C# Application with the web browser component, but can that intercept the browse button?
The only option that I can see working is using a C# Application with the web browser component. You can then use WebBrowser.ObjectForScripting to provide a method that can be called to trigger your custom picker window through Javscript, e.g:
window.external.ShowPickerWindow();
You then have two options:
Interrogate the DOM of the page once it's loaded and replace the button with one that triggers your picker window.
Have the customer change their page so it checks for the existance of a window.external.ShowPickerWindow method and basically does option (1) for you.
You can then have a method, perhaps called window.external.GetPickedCode() to pull the code out in the page.
Rob kinder steered me along the correct thinking track by saying "replace the button" which has lead me to a solution which works beautifully!
In short, I hide the browse button, insert a new button next to it that when clicked, opens a new window with a textbox. This textbox then sets a string value in the parent form which is used onSubmit to attach the file.
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement btnBrowse = wb.Document.GetElementById("fiPhoto");
if (btnBrowse != null)
{
HtmlElement newbtn = wb.Document.CreateElement("input");
newbtn.SetAttribute("id", "btnLoad");
newbtn.SetAttribute("type", "button");
newbtn.SetAttribute("value", "Load");
newbtn.Click += new HtmlElementEventHandler(newbtn_Click);
btnBrowse.Parent.AppendChild(newbtn);
btnBrowse.Style = "display:none";
}
HtmlElementCollection forms = wb.Document.Forms;
if (forms.Count > 0)
{
HtmlElement form = wb.Document.Forms[0];
form.AttachEventHandler("onsubmit", delegate(object o, EventArgs arg)
{
FormToMultipartPostData postData = new FormToMultipartPostData(wb, form);
postData.AddFile("photo", photo);
postData.Submit();
});
}
}
private void newbtn_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.ShowDialog();
}
FormToMultipartPostData is too big to post in here but it basically manually constructs the Content-Disposition to be posted
Don't show the actual file browser, imitate one which is showing only that one file in in.
Or since you know the file path when correct code is entered copy the file to temp folder you created and open file browser to browse that folder and it will be contain only that file.

Focusing WebBrowser control in a C# application

I have a WebBrowser control hosted in a windows Form. The control is used to display hyperlinks which get created at runtime. These links point to some HTML pages and PDF documents.
The problem is that when the form hosting the browser control is loaded, the focus is on the form. When the TAB key is pressed, the focus does not shift to the first hyperlink. However, if I perform a mouse click on the control and then hit the TAB key, the tab focus is now on the first hyper link. I tried using Select() on the WebBrowser control and then I called Focus(), but it doesn't solve the problem.
Any ideas on how to set the tab focus on the first hyperlink at load? Thanks.
Cheers,
Harish
I guess it might be because the focus is set before the page is fully loaded. Try this:
private void Go(string url)
{
webBrowser1.Navigate(url);
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.Body.Focus();
}
You could also automatically select the focus on the first link directly by getting the HtmlElement of that first link.
If the above doesn't work, you might want to check other parts of your code to see if anything else is capturing the focus. Try searching for Select, Focus and ActiveControl in your code.
Use form.ShowDialog(form) instead on form.Show(), then it will work !
where form is the running instance of your windows Form
This is my solution
private void txtAdres_KeyPress(object sender, KeyPressEventArgs e)
{
int licznik = 1;
if (e.KeyChar == (char)13)
{
string adres = txtAdres.Text;
webBrowser1.Navigate(adres);
licznik = 0;
}
if (licznik == 0)
{
webBrowser1.Focus();
}
}
In a normal scenario it should be sufficient for you to set the TabIndex of the WebBrowser control to zero. This way, when the form loads the control will be focused and pressing TAB will iterate through the links.
Note that you should also change the TabIndex of the other controls on the form.
If this does not solve your problem, you need to add more detail on the complexity of the form hosting the control.

List replies in a winform

In my C# app I get an xml from a server that contains some replies like in a forum thread (with elements like author, time, body, title, whatever).
When I get this xml, I create a new form in which i want to display these replies, and a little text box with an "add reply" button. I'd also like some edit buttons on perhaps my own replies in the reply list displayed in the form.
The simplest way that came to my mind to display the replies is to put a web browser control in the form, generate a full html page in a string from the xml, and throw it in that web browser control. And under it i can put the text box with the add reply button.
Everything is ok, except that i have no idea of how i could implement the edit function on my own replies (i mean i could add a link in there... but link to what)
I would like to know if there is a way to get that edit event from the web browser control (my guess is i can't) or another (maybe simple/easy) idea of displaying the replies in a winform using other controls
Yes, that's possible, you want to turn "design mode" on for the document. Add a reference to Microsoft.mshtml. Start a new Windows Forms project and drop a WB and a button on the form. Make the code look similar to this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
webBrowser1.DocumentText = "<html><body><textarea rows='15' cols='92' name='post-text' id='wmd-input'></textarea></body></html>";
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
button1.Click += button1_Click;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
mshtml.IHTMLDocument2 doc = webBrowser1.ActiveXInstance as mshtml.IHTMLDocument2;
doc.designMode = "On";
}
private void button1_Click(object sender, EventArgs e) {
var html = webBrowser1.Document.Body.All["post-text"].InnerHtml;
// do something with that
//...
}
}

Categories

Resources