在webrowser c#中查找文本 [英] find text in webrowser c#

查看:82
本文介绍了在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");
}

推荐答案

代码中的这两行是多余的,可能不是你想要的...... 。

These two lines in your code are redundant and are probably not what you intended...
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.


好的,我决定将问题的其他部分作为第二个解决方案发布。



请注意,如果在查找输入元素时未加载文档,则不会找到元素。在使用输入元素填充HtmlElementCollection之前,请确保已加载文档。您可以将例程附加到DocumentCompleted事件,该事件触发代码以检查输入元素。这样可以确保在查找元素之前加载文档。像这样...



创建一个例程来处理事件并调用你的例行程序来检查Book-it值

OK, I've decided to post the other part of your issue as a second solution.

Be aware that if the document isn't done loading when you look for the input elements then you will not find the element. Make sure the document has loaded before you fill your HtmlElementCollection with the input elements. You could attach a routine to the DocumentCompleted event that fires the code to check for the input element. This would ensure your documents loaded before you look for you element. Like so...

Create a routine to handle the event and call your routine that checks for the Book-it value
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#中查找文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆