0

im trying to click a button in my webbrowser.

i have created a button in my form to click that button, but it gives me error on this line

    Dim elements As HtmlElementCollection = custombrowser.Document.All

could it be that the website is blocking me for clicking that button?? here is my custom browser class:

Public Class custombrowser


Inherits WebBrowser
Public Sub New()
    Me.ScriptErrorsSuppressed = True
End Sub
Private Sub docomplited() Handles Me.DocumentCompleted
    Dim TP As TabPage = Me.Tag



    My.Settings.ddd = TP.Text
    If Me.DocumentTitle.Length > 15 Then
        TP.Text = Me.DocumentTitle.Substring(0, 14) & ""
    Else
        TP.Text = Me.DocumentTitle


    End If
    My.Settings.ddd = TP.Text


End Sub

Private Sub custombrowser_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged

End Sub


Private Sub custombrowser_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles Me.Navigated
    Form1.Label1.Text = Me.Url.ToString
    End Sub
End Class

The buttton code :

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


    If custombrowser.document IsNot Nothing Then
        Dim elements As HtmlElementCollection = custombrowser.Document.All



        For Each webpageelement As HtmlElement In elements
            custombrowser.Document.All("click").InvokeMember("click")
            If webpageelement.GetAttribute("class") = "click" Then

                webpageelement.InvokeMember("click")
            End If


        Next
    End If
End Sub
  • Where is the button code? – Nizam May 11 '15 at 20:00
  • here is the code after adding what you told me to do : If custombrowser.document IsNot Nothing Then Dim elements As HtmlElementCollection = custombrowser.Document.All For Each webpageelement As HtmlElement In elements bba.Document.All(click").InvokeMember("click") If webpageelement.GetAttribute("class") = "click" Then webpageelement.InvokeMember("click") End If Next End If – Pkcrown own May 11 '15 at 20:04
  • You are still having the same error? Where (exact point) are you getting the erro? – Nizam May 11 '15 at 20:10
  • now the new error is on this code : ( If custombrowser.document IsNot Nothing Then ) this is the error :Object variable or With block variable not set. – Pkcrown own May 11 '15 at 20:19
  • The custombrowser is the name of your class. The object should have name as `cutombrowser1` for example. Are you sure it is named `customborwser`? – Nizam May 11 '15 at 20:21
  • yes, it is. i have used it in other parts of the program , i have add the class code above. – Pkcrown own May 11 '15 at 20:24
  • I would set a breakpoint to know waht is nothing. Custombrowser is nothing, or is does not have document property. But my guess is that custombrowser is nothing. – Nizam May 11 '15 at 20:26
  • why do you think the custombrowser is the problem ?? because i have already used it before and there was no problem – Pkcrown own May 11 '15 at 20:30
  • This message of error means that the object is being used when it is nothing. If custombroswer is not nothing, then somthing it uses is: like Form1.Label1 (which should never be part of this class) – Nizam May 11 '15 at 20:34
  • ok so i deleted that part and i waited like 1 mintue beofre clicking the button to make sure that its loaded. – Pkcrown own May 11 '15 at 20:41
  • check this out http://prntscr.com/74581r its saying that the value of custombrowser is nothing – Pkcrown own May 11 '15 at 20:44
  • I reproduced here and discovered that your problem is Me.Tag is nothing. Then TP.Text is also nothing and cannot receive the value of Me.DocumentTitle – Nizam May 11 '15 at 20:49
  • That is what I was saying before. Select the broswer in your form and click F4. Look the (Name) property. Waht is the name? – Nizam May 11 '15 at 20:52
  • thanks for your help and time , i managed to find a way a round other than clicking the button. – Pkcrown own May 11 '15 at 21:00
  • Glad could help you any way. – Nizam May 11 '15 at 21:01
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp May 11 '15 at 21:20

0 Answers0