I am trying to automate fill the textbox of a website in c# and i used:
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.WebBrowser webBrowser = new WebBrowser();
HtmlDocument document = null;
document=webBrowser.Document;
System.Diagnostics.Process.Start("http://www.google.co.in");
document.GetElementById("lst-ib").SetAttribute("value", "ss");
}
The webpage is opening but the text box is not filled with the specified value. I have also tried innertext instead of setAttribute. I am using windows forms.
You are expecting that your webBrowser will load the page at specified address, but actually your code will start default browser (pointing at "http://www.google.co.in"), while webBrowser.Document will remain null.
try to replace the Process.Start with
webBrowser.Navigate(yourUrl);
Eliminate the Process.Start() statement (as suggested by Gian Paolo) because it starts a WebBrowser as an external process.
The problem with your code is that you want to manipulate the value of your element too fast. Wait for the website to be loaded completely:
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.WebBrowser webBrowser = new WebBrowser();
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
webBrowser.Navigate("http://www.google.co.in");
}
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser.document.GetElementById("lst-ib").SetAttribute("value", "ss");
}
Please note that using a instance of a WebBrowser is not often the best solution for a problem. It uses a lot of RAM and has some overhead you could avoid.
Related
I'm using the .NET WebBrowser control in a WinForms application to implement a very basic e-mail template editor.
I've set it in edit mode by this code:
wbEmailText.Navigate( "about:blank" );
( (HTMLDocument)wbEmailText.Document.DomDocument ).designMode = "On";
So the user can modify the WebBrowser content.
Now I need to detect when the user modifies the content 'cause I have to validate it.
I've tried to use some WebBrowser's events like DocumentCompleted, Navigated, etc. but no-one of these worked.
Could someone give me advice, please?
Thanks in advance!
I did have some working, really world code but that project is about 5 years old and I've since left the company. I've trawled my back-ups but can't find it so I am trying to work from memory and give you some pointers.
There are lots of events you can catch and then hook into to find out whether a change has been made. A list of the events can be found here: http://msdn.microsoft.com/en-us/library/ms535862(v=vs.85).aspx
What we did was catch key events (they're typing) and click events (they've moved focus or dragged / dropped etc) and handled that.
Some example code, note a few bits are pseudo code because I couldn't remember off the top of my head the actual code.
// Pseudo code
private string _content = string.empty;
private void frmMain_Load(object sender, EventArgs e)
{
// This tells the browser that any javascript requests that call "window.external..." to use this form, useful if you want to hook up events so the browser can notify us of things via JavaScript
webBrowser1.ObjectForScripting = this;
webBrowser1.Url = new Uri("yourUrlHere");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Store original content
_content = webBrowser1.Content; // Pseudo code
webBrowser1.Document.Click += new HtmlElementEventHandler(Document_Click);
webBrowser1.Document.PreviewKeyDown +=new PreviewKeyDownEventHandler(Document_PreviewKeyDown);
}
protected void Document_Click(object sender, EventArgs e)
{
DocumentChanged();
}
protected void Document_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
DocumentChanged();
}
private void DocumentChanged()
{
// Compare old content with new content
if (_content == webBrowser1.Content) // Pseudo code
{
// No changes made...
return;
}
// Add code to handle the change
// ...
// Store current content so can compare on next event etc
_content = webBrowser1.Content; // Pseudo code
}
I have this peculiar problem while wanting to print a html-report. The file itself is a normal local html file, located on my hard drive.
To do this, I have tried the following:
public static void PrintReport(string path)
{
WebBrowser wb = new WebBrowser();
wb.Navigate(path);
wb.ShowPrintDialog()
}
And I have this form with a button with the click event:
private void button1_Click(object sender, EventArgs e)
{
string path = #"D:\MyReport.html";
PrintReport(path);
}
This does absolutely nothing. Which is kind of strange... but things get stranger...
When editing the print function to do the following:
public static void PrintReport(string path)
{
WebBrowser wb = new WebBrowser();
wb.Navigate(path);
MessageBox.Show("TEST");
wb.ShowPrintDialog()
}
It works. Yes, only adding a MessageBox. The MessageBox is showing and after it comes the print dialog. I have also tried with Thread.Sleep(1000) instead, which doesn't work. Can anyone explain to me what's going on here? Why would a messagebox make any difference?
Can it be some kind of permission problem? I've reproduced this on both Windows 7 and 8, same thing. I made this small application with only the above code to isolate the problem. I am quite sure it works on windows XP though, since an older version of the application I'm working on runs on it. When trying to do this directly with the mshtml-dll instead I also get problems.
Any input or clarification is greatly appreciated!
The problem is that the browser is not ready to print yet. You will want to add an event handler WebBrowserDocumentCompletedEventHandler to the WebBrowser Object. Sample code below.
public static void PrintReport(string path)
{
WebBrowser wb = new WebBrowser();
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
wb.Navigate(path);
}
public static void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = (WebBrowser)sender;
if (wb.ReadyState.Equals(WebBrowserReadyState.Complete))
wb.ShowPrintDialog();
}
I have created a web browser in c#
this was what i get when i opened my web browser and typed google. Then i searched google for something
the result was like this
But the url wasn't updated in address bar. How to update the address bar when user click on a link on any website in my web browser
In the first image the url was google.com
In the second image url was https://www.google.co.in/#hl=en&output=search&sclient=psy-ab like that some thing but it wasn't updated
You must update the textbox on top with the URL of the WebBrowserControl, using the webBrowser1_Navigating event.
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
textbox1.text = webBrowser1.Url.ToString();
}
Check http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser_events.
I think you can use Navigating event to detect when user starts search or navigates to another page.
The Form_Load must contain this:
private void Form1_Load(object sender, EventArgs e)
{
web = new WebBrowser();
web.Navigated += web_Navigated;
}
and this function:
private void web_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
textBox1.Text = web.Url.ToString();
}
How to convert HtmlDocument.DomDocument to string?
This example is a bit convoluted, but, assuming you have a form called Form1, with a WebBrowser control called webBrowser1, the variable content will contain the markup that forms the document:
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri(#"http://www.robertwray.co.uk/");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var document = webBrowser1.Document;
var documentAsIHtmlDocument3 = (mshtml.IHTMLDocument3)document.DomDocument;
var content = documentAsIHtmlDocument3.documentElement.innerHTML;
}
The essential "guts" of extracting it from the HtmlDocument.DomDocument is in the webBrowser1_DocumentCompleted event handler.
Note: mshtml is obtained by adding a COM reference to 'Microsoft HTML Object Library` (aka: mshtml.dll)
It would be easier to use the HtmlDocument itself, rather than its DomDocument property:
string html = htmlDoc.Body.InnerHtml;
Or even simpler, if you have access to the WebBrowser containing the document:
string html = webBrowser.DocumentText;
I'm trying to make my own webbrowser with C#,
my wpf application seems to be correct. but it's still missing something.
the webpage doesn't appear. :s
Does someone have an idea?
Here's my code in C# :
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void button1_Click(object sender, RoutedEventArgs e)
{
WebBrowser web = new WebBrowser();
web.NavigateToString (textBox1.Text);
}
Thanks for your help.
As I understand, you are instantiating a new WebBrowser control in code and you aren't adding it as a control to the actual form. You'd better add the control in design view and just do the method call in the code.
When you create the WebBrowser, try adding a third line:
WebBrowser web = new WebBrowser();
Content = web; // extra line
web.NavigateToString (textBox1.Text);
If the textbox is your address bar, it won't work. NavigateToString will interpret what's in your textbox as literal HTML.
web.NavigateToString (textBox1.Text);
should be
web.Source = new Uri(textBox1.Text, UriKind.Absolute);