0

I am trying to learn WebBrowser control in windows forms.

I am able to make a search and display the search result page. Now I have to navigate through all the pages in the search result. I have got reference of the unordered list. What is the best way to get all the children and call their click evnt (so that it will navigate through each page)?

public partial class Form1 : Form
{

    string websiteUrl = @"https://www.bing.com/";

    private System.Windows.Forms.WebBrowser wb = null;
    private Button button1 = null;
    private ListBox listBox1 = null;

    public Form1()
    {
        // button1
        button1 = new Button();
        button1.Location = new Point(20, 430);
        button1.Size = new Size(90, 23);
        button1.Text = "Load and Test";
        button1.Click += new EventHandler(this.button1_Click);

        // listBox1
        listBox1 = new ListBox();
        listBox1.Location = new Point(10, 460);
        listBox1.Size = new Size(460, 200);

        // Web Browser
        wb = new WebBrowser();
        wb.Location = new Point(10, 10);
        wb.Size = new Size(1000, 400);

        //Subscribing for the Document Completed Event
        wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(ExerciseApp);

        // Form1
        this.Text = "Web Browser Test";
        this.Size = new Size(5000, 7100);
        this.Controls.Add(wb);
        this.Controls.Add(button1);
        this.Controls.Add(listBox1);
    } 

    private void button1_Click(object sender, EventArgs e) 
    {
        listBox1.Items.Add("Loading Web app under test into WebBrowser control");
        wb.Url = new Uri(websiteUrl);

    }

    private void ExerciseApp(object sender, EventArgs e)
    {
        HtmlElement textEntryLocation = this.wb.Document.GetElementById("sb_form_q");
        textEntryLocation.InnerText = "Color";

        HtmlElement btnSearch = this.wb.Document.GetElementById("sb_form_go");
        btnSearch.InvokeMember("click");

        Thread.Sleep(500);

        wb.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(ExerciseApp);

        //Get the unordered list
        HtmlElement unorderedList = this.wb.Document.GetElementById("sb_pagF");
    }
}
noseratio
  • 59,932
  • 34
  • 208
  • 486
LCJ
  • 22,196
  • 67
  • 260
  • 418

0 Answers0