c# - Select specific text boxes on WebBrowser and write in them - c#

how would I select a specific text box on a web browser (the default c# webbrowser) and write a string in it? I have been thinking of webrequests but that may be illegal due to the amount of packets you will send to the host server.

If you are just wanting to fill in a field, you need to use the DOM. Here are some examples that assume your browser is named browser and has navigated to google.com.
WPF
mshtml.IHTMLDocument2 htmlDoc = browser.Document as mshtml.IHTMLDocument2;
((mshtml.HTMLInputElement)htmlDoc.all.item("lst-ib")).value = "wpf web browser access dom";
WinForms
HtmlDocument htmlDoc = browser.Document;
htmlDoc.All["lst-ib"].InnerText = "c# web browser access dom";

Related

Is there an alternative to WebBrowser control for DOM traversal?

I am currently using a WebBrowser control in my Windows Forms application to navigate to a URL. Once I am at that URL, I use the FirstChild in conjunction with NextSibling methods of the HtmlElement class to walk the document tree from the WebBrowser.Document object.
The reason I do this is to get information from a page and store this information into a database.
Here is the crux of my question: Do I really need to use the WebBrowser class? I currently do not need to display the web page to the user, only some of the information found in the page.
Is there a better way to do this without relying on this class? Something solid which can do DOM traversal would be required, but as mentioned above, I do not need to display the web page.
Regards
Crouz
You can use a WebClient to download the HTML without displaying the page. You can then use something like HTML Agility Pack to create an HTMLDocument from the string.
Example:
using (WebClient wc = new WebClient())
{
string html = wc.DownloadString("http://www.foo.bar/"); // Change as required.
HtmlAgilityPack.HtmlDocument h = new HtmlAgilityPack.HtmlDocument();
h.LoadHtml(html);
}
Reason to use HTML Agility Pack:
The HtmlDocument class is a wrapper around the native IHtmlDocument2 COM interface.
You cannot easily create it from a string.....
and thus not without using the WebBrowser.
From https://stackoverflow.com/a/4935482/4546874.
However, you can hide the WebBrowser.

C# WebBrowser control - creating and modifying a javascript variable using DOM then reading it using Applet

I need to make a communication process between java applet and C# WebBrowser control's html page. And I want to do it without refreshing the html page. I know I can communicate with applet using applet parameters, but in that case I have to refresh everytime the applet page to get updated parameter. I also can use cookie, but I dont want to send all those unnecessary cookies to server for each request. So I was thinking if there is a way to create javacript array variable using DOM and then read it with the applet. But I dont know if it is possible or may be there are other ways to do it. Any suggestion will be highly appreciated.
Thanks
You can achieve this by injecting html code in the Webrowser Controls DocumentText property
string htmlCode = "<html><head></head><body>";
htmlCode += "<applet code="Example.class" width="350" height="350"></applet>";
htmlCode +="</body></html>";
webBrowser1.DocumentText = htmlCode;

Opening a browser and filling in some data

i want to know how i can open a browser to a specific web page and then fill out some of the content of the boxes on that page.
My idea is for someone to be able to order a particular item from our internal ordering system. The barcodes for these items are what will populate the fields on the page i want to open.
I no i can open a new instance of ie using Process.Start("IEXPLORE.EXE", url); howver how do i get a handle on that exact ie instance window so i can begin to add the required data to the fields?
Is this even possible?
Thanks very much
WatiN should help with this. I've generally used it for acceptance testing of web apps, but the principle is the same. Open a browser instance, reference stuff in the DOM, manipulate form elements, etc.
In addition to WatiN (as was suggested in another answer), you might consider a load testing package like Web Performance Load Tester. They have a free version that lets you run up to 10 virtual users at a time, which will perform scripted actions.
Another option would be to use a standard WebBrowser object to load your website. The WebBrowser object allows you to access and alter certain web parts. Below is sample code that automatically searches Bing:
private void LoadPage()
{
WebBrowser webBrowser1 = new WebBrowser();
webBrowser1.Navigate("http://www.bing.com");
//Wait for document to load...
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
//Set the text of the search input
HtmlElement txtTextField = webBrowser1.Document.GetElementById("sb_form_q");
txtTextField.InnerText = "My test text";
//Perform a click on the search button
HtmlElement btnSubmit = webBrowser1.Document.GetElementById("sb_form_go");
btnSubmit.InvokeMember("click");
}

Embedded WebBrowser in Windows Form C# project

I have a form with an embedded web browser control on it. I am currently using WebBrowser and use it like so:
webBrowser1.Navigate("about:blank");
HtmlDocument doc = this.webBrowser1.Document;
doc.Write(string.Empty);
String htmlContent = GetHTML();
doc.Write(htmlContent);
This writes the HTML correctly to the web browser control BUT it never clears the existing data and it just appends, so I end up with N web pages stacked on top of each other.
Is this the best control to use? If so why is it not clearing existing data?
You need to use:
HtmlDocument doc = this.webBrowser1.Document.OpenNew(true);
now the contents of the document will be cleared before writing.
All calls to Write should be preceded
by a call to OpenNew, which will clear
the current document and all of its
variables. Your calls to Write will
create a new HTML document in its
place. To change only a specific
portion of the document, obtain the
appropriate HtmlElement and set its
InnerHtml property.
Yes, it is.
You should be able to call the Clear method if you need to clear contents.
Check this article for in-depth details and sample code:
http://www.codeproject.com/KB/miscctrl/simplebrowserformfc.aspx
Call HtmlDocument.OpenNew between pages:
OpenNew will clear the previous loaded
document, including any associated
state, such as variables. It will not
cause navigation events in WebBrowser
to be raised.

Read HTML code from iframe using Webbrowser C#

How to read IFRAME html code using WebBrowser?
I have site with iframe, and after few clicks new URL opens inside this IFRAME with some portion of HTML CODE. Is there a possiblity to read this?. When I am trying to Navigate() to this URL, I am redirected to main page of this site (it is not possible to open this link twice).
Uri IFRAME_URL = webBrowser1.Document.Window.Frames[0].Url;
Maybe there is something similar to:
Uri IFRAME_URL = webBrowser1.Document.Window.Frames[0]. ... DOCUMENTTEXT;
Try:
string content = webBrowser1.Document.Window.Frames[0].WindowFrameElement.InnerText;
You can also acquire various items via mshtml types:
Set a reference to the "Microsoft HTML Object Library" under COM references.
Set your using statement:
using mshtml;
Then tap into the mshtml API to snatch the source:
HTMLFrameBase frame = yourWebBrowserControl.Document.GetElementById( "yourFrameId" ).DomElement as HTMLFrameBase;
If "frame" isn't null after that line, it has a lot of items hanging off it for your use.
try:
string content = webBrowser1.Document.Window.Frames[0].Document.Body.InnerText
A WebBrowser Control window can contain more that one iframe and .net supports frame collection so why not use something like this:
// Setup a string variable...
string html = string.Empty;
// webBrowser1.Document.Window.Frames gets a collection of iframes contained in the current document...
// HTMLWindow is the iterator for the Collection...
foreach (HtmlWindow frame in webBrowser1.Document.Window.Frames)
{
html += frame.Document.Body.OuterHtml;
}
This way, maybe with a little adjustment you can get all you need from the iframe containers you need.

Categories