本文介绍了在webrowser c#中查找文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好;



找到按钮webbrowser:

hello;

find button webbrowser:

<input type="submit" value="Book it" class="button-book-it">





error在我的代码中:



error in my code:

HtmlElementCollection elc = this.webBrowser1.Document.GetElementsByTagName("input");
HtmlElement el = this.webBrowser1.Document.GetElementsByTagName("input");
if (el.GetAttribute("value").Contains("Book it") == true)
{
    MessageBox.Show("find");
    break;
}
else
{
    MessageBox.Show("no");
}

推荐答案

HtmlElementCollection elc = this.webBrowser1.Document.GetElementsByTagName("input");
HtmlElement el = this.webBrowser1.Document.GetElementsByTagName("input");



上面的第二行正是在做第一行,但是将结果放在另一个对象中(顺便错误的类型,GetElementsByTagName()返回一个元素集合而不是单个HtmlElement。



你想得到一个输入元素的集合,就像第一个行呢。但是你想要使用该系列来获得该系列中的特定输入项目。



您有多种选择可以做到这一点...



选项#1 - 此选项适用于您在代码中尝试的内容。但它假设我们总是想要第一个输入元素


The second line above is doing exactly what the first is doing but putting the result in a different object (of the wrong type by the way, GetElementsByTagName() returns a collection of elements not a single HtmlElement).

You want to get a collection of the input elements, like the first line does. But then you want to work with the collection to get the specific input item from the collection.

You have multiple options to do this...

Option #1 - This option is closet to what you are trying in your code. But it assumes that we always want the first input element

// get collection of input elements
HtmlElementCollection elc = webBrowser1.Document.GetElementsByTagName("input");

// get the first element if the collection isn't empty
if (elc != null && elc.Count > 0)
{
    HtmlElement el = elc[0]; // get first item
    // check attribute value
    if (el.GetAttribute("value").Contains("Book it") == true)
    {
        MessageBox.Show("find");
    }
    else
    {
        MessageBox.Show("no");
    }
}





选项#2 - 此选项使用循环检查集合中的所有输入控件并假设我们要查找的值可以是一个或多个输入元素的一部分



Option #2 - This option uses a loop to check all input controls in the collection and assumes the value we are looking for can be part of one or more of the input elements

HtmlElementCollection elc = webBrowser1.Document.GetElementsByTagName("input");

foreach (HtmlElement el in elc)
{
    if (el.GetAttribute("value").Contains("Book it") == true)
    {
        MessageBox.Show("find");
    }
}





选项#3 - 此选项使用LINQ来压缩代码,实质上是做什么的选项#2但代码较少。



Option #3 - This option uses LINQ to condense the code and is essentially doing what option #2 does but with less code.

HtmlElementCollection elc = webBrowser1.Document.GetElementsByTagName("input");
HtmlElement el = (from HtmlElement eli in elc
                  where eli.GetAttribute("value") == "Book it"
                  select eli).FirstOrDefault();

if (el != null)
{
     MessageBox.Show("find");
}





希望这会有所帮助。



Hope this helps.


private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    // do test for Book-it value
}



将您的例程附加到WebBrowser控件的DocumentCompleted事件...


Attach your routine to the DocumentCompleted event of the WebBrowser control...

public Form1()
{
    webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler webBrowser1_DocumentCompleted);
}


这篇关于在webrowser c#中查找文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:09