Răzvan Bălan February 2016

Search for certain string in InnerHtml file using webbrowser

I am a beginner in c# and i don't seem to manage to scrap an innerHtml from a website. The website is a ticket management system which has tickets entered by ID. After entering the ID, i would like some information to be displayed in a Windows form application textboxes: ticket title, created at, and so on..

Untill now, I used webbrowser in order to log in on the site and then i want to enter a ticket ID into a textbox and after pressing Generate button i want to display the ticket title into a textbox.

Here is my code: // for the log in button

this.webBrowser1.Navigate("https://elvis.harman.com/");

            while (this.webBrowser1.ReadyState != WebBrowserReadyState.Complete)
            {
                Thread.Sleep(100);
                Application.DoEvents();
            }

            //textBox1.Text= this.webBrowser1.Document.Window.Frames[2].Document.GetElementsByTagName("input").Count.ToString();


            this.webBrowser1.Document.Window.Frames[2].Document.GetElementsByTagName("input")[0].InnerText = textBox1.Text;
            this.webBrowser1.Document.Window.Frames[2].Document.GetElementsByTagName("input")[1].InnerText = textBox2.Text;
            this.webBrowser1.Document.Window.Frames[2].Document.GetElementsByTagName("button")[0].InvokeMember("click");

// for the generate button

    this.webBrowser1.Navigate("https://elvis.harman.com/cgi-bin/ticket?TID=" + txtID.Text);
this.webBrowser1.Update();
this.webBrowser1.Refresh();
var codes = this.webBrowser1.Document.GetElementById("field_100").InnerHtml.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(m => m.Trim()).ToList(); 
// with this i tried to separate the innerHtml by lines

//and now i try to start scrapping beggining with a following line:
 var ticketLines = codes.Where(m => m.StartsWith("<TD style=\"WIDTH: 65px; HEIGHT: 22px; LEFT: 110px\" class=hb-table-cell>"));
List<s        

Answers


Răzvan Bălan February 2016

I managed to solve this and i will post it here so that this post should have a reason :)

foreach (string line in ticketLines)
            {   
                if (line.Contains(searchkeys)) 
                {
                    //daca se gaseste ce e in txtID se ia indexul liniei si il bag in txtTitle.text
                    int y = codes.IndexOf(line);
                    txtTitle.Text = codes[y+1].Substring(codes[y + 1].IndexOf(">") + 1, codes[y + 1].Length - codes[y + 1].IndexOf(">") - 6);
                    txtCreatedAt.Text = codes[y + 3].Substring(codes[y + 3].IndexOf(">") + 1, codes[y + 3].Length - codes[y + 3].IndexOf(">") - 6);
                    txtCreatedBy.Text = codes[y + 4].Substring(codes[y + 4].IndexOf(">") + 1, codes[y + 4].Length - codes[y + 4].IndexOf(">") - 6);

                    break;
                }


            }

Post Status

Asked in February 2016
Viewed 1,043 times
Voted 14
Answered 1 times

Search




Leave an answer