Jack J Jack J - 1 month ago 22
C# Question

reading data off website in C#

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
try
{
HtmlDocument doc = logger.Document;
HtmlElement username = doc.GetElementById("loginUsername");
HtmlElement password = doc.GetElementById("loginPassword");
HtmlElement submit = doc.GetElementById("loginSubmit");
username.SetAttribute("value", "myusername");
password.SetAttribute("value", "mypassword");
submit.InvokeMember("click");
}
catch
{

}
}


Hello, I am trying to make a program that will log onto a website, and read some texts off to display it to the user. With the code above, I've made the webbrowser log in automatically so far.

On the page after logging in,

enter image description here

I want the program to click on the Timetable element. But it doesn't have its own id. How can I get access to it?

Is this even a right way to do it? How can I achieve what I want here? I couldn't do it myself so I put this question here!

Answer

You don't need to click on the button, as you have the URL. You can call the URL directly and read the code that has been returned:

var req = (HttpWebRequest)WebRequest.Create(URL);
req.Method = "GET";
var resp = req.GetResponse();

using(var sr = new StreamReader(resp.GetResponseStream(), System.Text.Encoding.UTF8))
{
    string result = sr.ReadToEnd();
}

myResponse.Close();

You can see the data returned if you use a tool such as Fiddler.