循环浏览WebBrowsers

时间:2016-09-08 06:26:28

标签: c#

此代码适用于单个webBrowser1

    HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("a");

    foreach (HtmlElement link in links)
    {
        if (link.OuterHtml.ToString().Contains("E-mail"))
        {
            link.InvokeMember("click");
        }
    }

但是我想将它用于每个webBrowser,所以我提到了类似的东西:

    foreach (Control c in this.Controls)
    {

        WebBrowser t = c as WebBrowser;

        HtmlElementCollection links = t.Document.GetElementsByTagName("a");
        foreach (HtmlElement link in links)
        {
            if (link.OuterHtml.ToString().Contains("E-mail"))
            {
                link.InvokeMember("click");
            }
        }
    }

但它说"Object reference not set to an instance of an object"

1 个答案:

答案 0 :(得分:2)

public static void checkMatrix(int[][] intMatrix) { File numberFile = new File("valid3x3") ; intMatrix= readMatrix(numberFile); int nSquared = sideLength * sideLength; int[] values = new int[nSquared]; int[] range = new int[nSquared]; int valCount = 0; for (int i = 0; i<sideLength; i++) { for (int j=0; j<sideLength; j++) { values[valCount] = intMatrix[i][j]; valCount++; } } for (int i=0; i<range.length; i++) { range[i] = i+1; } Boolean valuesThere = hasRange(values, range); 是与Web浏览器不同的控件,因此c强制转换失败。您可以使用OfType仅获取网络浏览器

as

否则您需要处理foreach (var c in this.Controls.OfType<WebBrowser>())

NullReferenceException