Click here to Skip to main content
16,016,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello;

find button webbrowser:
HTML
<input type="submit" value="Book it" class="button-book-it">


error in my code:
C#
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");
}
Posted
Comments
Richard C Bishop 15-Aug-13 18:09pm    
What error?
samadblaj 15-Aug-13 18:17pm    
not found.
Richard C Bishop 15-Aug-13 18:17pm    
what is not found?
samadblaj 15-Aug-13 18:22pm    
this page search "book it"?
samadblaj 15-Aug-13 18:09pm    
find :Book it

These two lines in your code are redundant and are probably not what you intended...
C#
HtmlElementCollection elc = this.webBrowser1.Document.GetElementsByTagName("input");
HtmlElement el = this.webBrowser1.Document.GetElementsByTagName("input");

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


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
C#
HtmlElementCollection elc = webBrowser1.Document.GetElementsByTagName("input");

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


Option #3 - This option uses LINQ to condense the code and is essentially doing what option #2 does but with less code.
C#
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.
 
Share this answer
 
v3
Comments
idenizeni 15-Aug-13 19:53pm    
One other thing, 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.
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
C#
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    // do test for Book-it value
}

Attach your routine to the DocumentCompleted event of the WebBrowser control...
C#
public Form1()
{
    webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler webBrowser1_DocumentCompleted);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900