I need to get all data from a website, so I need to walk through all the links. Your way worked, but if someone knows another way, will be also good.
Another problem.
My code is executed on the Document_Completed event, but it is being executed more than once. Are there any other way And how is the best way to handle progress change Is it the progress_changed event handler
The following code will "nearly click" on the first link on the document loaded in your web browser control. The "clicking" is simulated using the enter key which, when a link is focused, performs exactly as clicking it! only the concept is shown here. Now, you didn't clarify which link you wanted to click, but you can easily change the code. Just obtain the appropriate link element in the "ele" reference and use the same code. For example, you might iterate through the collection of all links in the document "es" and search for your own link.
The Code:
HtmlElementCollection es = webBrowser1.Document.GetElementsByTagName("a"); if (es != null && es.Count != 0) {
HtmlElement ele = es[0]; //This line is optional, it only visually scolls the first link element into view ele.ScrollIntoView(true); ele.Focus(); SendKeys.Send("{ENTER}");
}
Hope this helps!
Note to Dave: Close Enough! Although i agree with you on not seing a use for it!
To crawl through all the links in a web page, you dont' need clicking on each link! You have the webBrowser1.Document object through which you can get all links in a page in many ways and then manipulate them as you like. For example:
You can put the following code in the evnt handler of a crawl button or the Navigated event of a web browser control to get all links:
HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("a"); foreach (HtmlElement ele in links) {
//Form3 is a form that contains a web browser control, an address combo box //a GO button and a "Crawl Links" button. Besides it has the NavigateTo() method declared later //However, you can do whatever you want here! Maybe open the links in the same window .. whatever Form3 frmNew = new Form3(); frmNew.Owner = this; frmNew.Show(); frmNew.NavigateTo(ele.GetAttribute("href"));
}
The NavigateTo method is included here:
public void NavigateTo(string url) { if(url != null && url != "" && Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute)) webBrowser1.Url = new Uri(url); }
The Navigating and Navigated events are good places to think of for placing your code:
This will update the title of the including form and the address combo box when the browser is at a certain site. This is for demonstration purposes!
The ProgressChanged event is indeed the right place to handle progress changes. I included a progress bar in my form and it is initailly set to Visible= false; It is only shown when a page is loaded and the progress is taken from the event args of the event:
Someone correct me if I am wrong, but I don't think this is possible in .NET. I don't know if it would be of any help, but you could use the Navigate() function.
I am not using webbrowser control. Through console application I have opened IE and one web based application in that IE. Now i want to click on links present on that application. how can click on links
Click on a link programmatically using WebBrowser control
Leo Liu
I need to get all data from a website, so I need to walk through all the links. Your way worked, but if someone knows another way, will be also good.
Another problem.
My code is executed on the Document_Completed event, but it is being executed more than once. Are there any other way And how is the best way to handle progress change Is it the progress_changed event handler
Thanks.
PSDCHD
The following code will "nearly click" on the first link on the document loaded in your web browser control. The "clicking" is simulated using the enter key which, when a link is focused, performs exactly as clicking it! only the concept is shown here. Now, you didn't clarify which link you wanted to click, but you can easily change the code. Just obtain the appropriate link element in the "ele" reference and use the same code. For example, you might iterate through the collection of all links in the document "es" and search for your own link.
The Code:
HtmlElementCollection es = webBrowser1.Document.GetElementsByTagName("a");if (es != null && es.Count != 0) { HtmlElement ele = es[0];
//This line is optional, it only visually scolls the first link element into view
ele.ScrollIntoView(true);
ele.Focus();
SendKeys.Send("{ENTER}");
}
Hope this helps!
Note to Dave: Close Enough! Although i agree with you on not seing a use for it!
Timm Hagen
To crawl through all the links in a web page, you dont' need clicking on each link! You have the webBrowser1.Document object through which you can get all links in a page in many ways and then manipulate them as you like. For example:
You can put the following code in the evnt handler of a crawl button or the Navigated event of a web browser control to get all links:
HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("a");foreach (HtmlElement ele in links) { //Form3 is a form that contains a web browser control, an address combo box
//a GO button and a "Crawl Links" button. Besides it has the NavigateTo() method declared later
//However, you can do whatever you want here! Maybe open the links in the same window .. whatever
Form3 frmNew = new Form3(); frmNew.Owner = this;
frmNew.Show(); frmNew.NavigateTo(ele.GetAttribute("href"));
}
The NavigateTo method is included here:
public void NavigateTo(string url) {if(url != null && url != "" && Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
webBrowser1.Url = new Uri(url);
}
The Navigating and Navigated events are good places to think of for placing your code:
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e) { this.Text = "Viewing: " + webBrowser1.Document.Title;comboBox1.Text = webBrowser1.Document.Url.ToString();
}
This will update the title of the including form and the address combo box when the browser is at a certain site. This is for demonstration purposes!
The ProgressChanged event is indeed the right place to handle progress changes. I included a progress bar in my form and it is initailly set to Visible= false;
private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e) { if (!progressBar1.Visible && e.CurrentProgress != e.MaximumProgress) { progressBar1.Visible = true; }It is only shown when a page is loaded and the progress is taken from the event args of the event:
if (progressBar1.Maximum != e.MaximumProgress) { progressBar1.Maximum = (int)e.MaximumProgress; }
progressBar1.Value = (int)e.CurrentProgress;
}
false;private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
}
if (comboBox1.Text != null) { true;//This is the event handler for the GO button that takes you to the specified Address
private void button1_Click(object sender, EventArgs e) {
webBrowser1.Url = new Uri(comboBox1.Text);
}
}
The above three handlers show how you coordinate between the three events!
Hope this code helped you answer what you have in mind!
IamWasim
Hey, thank you very very much.
Your code will help me with my project....thanks.
;-)
Imanol
jan_bp
I am not using webbrowser control. Through console application I have opened IE and one web based application in that IE. Now i want to click on links present on that application. how can click on links
elpepe