获取子控件并在WebBrowser控件中调用单击

时间:2014-03-20 20:51:18

标签: c# .net winforms webbrowser-control

我正在尝试在Windows窗体中学习WebBrowser控件。

我可以进行搜索并显示搜索结果页面。现在我必须浏览搜索结果中的所有页面。我已经参考了无序列表。让所有孩子接触并点击他们的点击的最佳方法是什么(以便它将浏览每一页)?

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");
    }
}

0 个答案:

没有答案