HelpC# msHtml problem.

Posts 1–6 of 6 · Page 1 of 1
C# msHtml problem.
Hello, im cuncurently working on a chatbot for a chatsite. But i got a problem with accesing the button with the value ("Start new chat")

on the line: foreach (HTMLInputElement child in elementCollection) the debugger gives me an error: HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))



Code:
private void button1_Click(object sender, EventArgs e)
        {
            HTMLDocument MyDoc = (HTMLDocument)ie.Document;
            HTMLElementCollection elementCollection = (HTMLElementCollection)MyDoc.getElementById("logbox");
            foreach (HTMLInputElement child in elementCollection)
            {
                if (child.value == "Start new chat")
                    child.click();
                
            }
        }
Ive searched on google for a awnser, But with no succes.

I hope somebody can help me with this error..
GetElementById

Not

GetElementsById

It returns a single item. Why are you treating it like a collection? No loop is needed if you're using GetElementById.
Quote Originally Posted by abuckau907 View Post
GetElementById

Not

GetElementsById

It returns a single item. Why are you treating it like a collection? No loop is needed if you're using GetElementById.
Becuase i cant acces the button by its id.

Its a childElement of the logbox. so i have to use a loop trough the logbox element.

But there is no good example on the web to actually do something with the element the loop is looking for.
Why can't you access the button by its id?
Its a childElement of the logbox. so i have to use a loop trough the logbox element.
By that logic EVERYTHING is a "child" of the <html> root element and you couldn't access anything by it's id. (?)



please post the html code for the <form>. Erase any parts we don't need to see (hardcoded styling, etc).

something very similar to
Code:
<form id="logbox" method="post">
...
<input type=submit value="Start new chat" />
</form>
so we can see the names and IDs of the controls, and their layout.



I haven't used these classes/functions in a while so I made up a demo to test...maybe it helps give you ideas..

http://tinypic.com/r/20zvcr8/8


 
text copy of vb.net code above, 99.9% the same as c# (in this example)

Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Navigate("C:/Users/Buckau/Desktop/index.html")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim _entirePage As HtmlDocument = WebBrowser1.Document

        ''Find the login <form>
        Dim _logBox As HtmlElement = _entirePage.GetElementById("logbox")
        If _logBox Is Nothing Then
            MsgBox("Log box not found!")
            Exit Sub
        End If

        ''We found it
        MsgBox("_logBox.InnerText: " & Environment.NewLine & _logBox.InnerText _
             & Environment.NewLine & Environment.NewLine _
             & "_logBox.InnerHtml: " & Environment.NewLine & _logBox.InnerHtml)
        If _logBox.Children.Count > 0 Then
            ''Loop over it's children 
            Dim _tmpStr As String = ""
            For Each xx As HtmlElement In _logBox.Children
                _tmpStr = xx.GetAttribute("value")
                If String.IsNullOrEmpty(_tmpStr) Then
                    MsgBox("input doesn't have a 'value' item" & Environment.NewLine _
                         & "name: " & xx.Name)
                Else
                    MsgBox("name: " & xx.Name & Environment.NewLine _
                          & "value = " & _tmpStr)
                End If
            Next
        Else
            MsgBox("The _logBox has No Children Elements! ie. an empty <form></form>.")
        End If

        MsgBox("Done analyzing") ''
    End Sub

The test html file I practiced on:
Code:
<html>
<head>
<title>yunoTest</title>
</head>
<body>

<form id="logbox" method="post">

<input type="text" name="email" value="youremail@mail.com" /><br />
<input type="password" name="password" value="********" /><br />
<input type=submit value="Start new chat" />

</form>

</body>
</html>
tip: Use HtmlElement instead of HtmlInputElement (unless you KNOW all your items will be <input> elements). GetAttribute() is useful..but make sure it returns something other than empty string.
Find the logbox form based on it's ID, then loop over its children, looking for one who has a defined attribute named 'value', with the value "Start new Chat": this is your button.
Look forward to your reply.
Also you must use Name, than not exist ID

Quote Originally Posted by vitaminb2 View Post
Hello, im cuncurently working on a chatbot for a chatsite. But i got a problem with accesing the button with the value ("Start new chat")

on the line: foreach (HTMLInputElement child in elementCollection) the debugger gives me an error: HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))



Code:
private void button1_Click(object sender, EventArgs e)
        {
            HTMLDocument MyDoc = (HTMLDocument)ie.Document;
            HTMLElementCollection elementCollection = (HTMLElementCollection)MyDoc.getElementById("logbox");
            foreach (HTMLInputElement child in elementCollection)
            {
                if (child.value == "Start new chat")
                    child.click();
                
            }
        }
Ive searched on google for a awnser, But with no succes.

I hope somebody can help me with this error..
Thanks for the reply's. Ive managed to get it working with a timer that constanly checks if the chat has been stopped. if it does, the program presses the button
Posts 1–6 of 6 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

Talk with us