0

I'm coding an A.I. in Visual Basic and when it when it doesn't know something the user inputted, it'll send the user's input into a Google form and submit it automatically in the background if they opted in.

However, I've been struggling to get this to work for many hours now, because whenever I attempt to reference the button in Visual Basic, I receive a null exception.

I've tried to reference the button by its class, but apparently I still run into a null exception error. I have verified that the webBrowser control is navigating the page before attempting to find the button. I expected there to be no errors and for the form to be submitted successfully.

wbFeedback.Navigate("https://docs.google.com/forms/d/e/. . . . . ./viewform?usp=pp_url&entry.1619369433=" + tbInput.Text.Replace(" ", "%20"))

tbInput is a text box that the user input's their question into for processing.

Here's the submit button's code:

<div role="button" class="uArJ5e UQuaGc Y5sE8d VkkpIf NqnGTe M9Bg4d" jscontroller="VXdfxd" jsaction="click:cOuCgd; mousedown:UX7yZ; mouseup:lbsD7e; mouseenter:tfO1Yc; mouseleave:JywGue;touchstart:p6p2H; touchmove:FwuNnf; touchend:yfqBxc(preventMouseEvents=true|preventDefault=true); touchcancel:JMtRjd;focus:AHmuwe; blur:O22p3e; contextmenu:mg9Pef;" jsshadow="" jsname="M2UYVd" tabindex="0">
<div class="Fvio9d MbhUzd" jsname="ksKsZd"></div>
<div class="e19J0b CeoRYc"></div>
<span jsslot="" class="l4V7wb Fxmcue">
<span class="NPEfkd RveJvd snByac">Submit</span>
</span>
</div>
Nimantha
  • 6,405
  • 6
  • 28
  • 69
RealCraze
  • 1
  • 2
  • Is this any use to you? https://stackoverflow.com/questions/52398172/how-to-fill-google-form-with-c-sharp or possibly https://stackoverflow.com/questions/33865480/how-do-you-post-data-to-a-multi-page-google-form – Andrew Mortimer Apr 05 '22 at 17:41
  • @AndrewMortimer They appear to be written for C#, which I tried to use as a guide since C# and Visual Basic have similarities, but I unfortunately couldn't get it to work out because C# uses different data calls, for example, "NameValueCollection" appears to be non-existent. This could totally be useful for others who are fluent in C# and Visual Basic possibly, but I couldn't work the translating C# to Visual Basic side of things out. – RealCraze Apr 06 '22 at 13:04
  • Import System.Collections.Specialized to get NameVauleCollection working. – Andrew Mortimer Apr 06 '22 at 13:12
  • Everything seemed to look right until I wrote `byte[] response = client.UploadValues(uri, "POST", keyValue)`, the issue seemed to be with `Byte[]` being used as an expression. – RealCraze Apr 06 '22 at 15:10
  • That'll be something like : Dim response As Byte() = client.UploadValues(Uri, "POST", keyValue) – Andrew Mortimer Apr 06 '22 at 16:07

1 Answers1

0

I ended up with the following:

Imports System.IO
Imports System.Text
Imports System.Web
Imports System.Net
Imports System.Windows.Forms.Timer
Imports System.Collections.Specialized

tmrFeedbackSubmissionSuccess.Interval = 2000 ' Remove if you don't want to use timers to display a label temporarily
tmrFeedbackSubmissionFailed.Interval = 10000 ' Remove if you don't want to use timers to display a label temporarily

Dim client As New WebClient
Dim keyValue As New NameValueCollection
Dim postdata As String
Dim displayError As Boolean = False

lblFeedbackSubmission.ForeColor = Color.ForestGreen
lblFeedbackSubmission.Text = "Sending..."
lblFeedbackSubmission.Visible = True

postdata = tbInput.Text.ToString()
keyValue.Add("entry.xxxxxxxxxx", postdata)
'keyValue.Add("entry.xxxxxxxxxx", postdata)
'keyValue.Add("entry.xxxxxxxxxx", postdata)

' You can add more entries, I only needed one in my case because there was only one input field on the form

Try
    client.UploadValues("https://docs.google.com/forms/u/0/d/e/. . . . . ./formResponse", "POST", keyValue)

Catch ex As WebException
    lblFeedbackSubmission.ForeColor = Color.Red
    lblFeedbackSubmission.Text = String.Format("Error: {0}", ex.Message)
    tmrFeedbackSubmissionFailed.Start() ' Remove if you don't want to use timers to display a label temporarily
    displayError = True
End Try

If displayError = False Then
    lblFeedbackSubmission.Text = "Sent Successfully!"
    tmrFeedbackSubmissionSuccess.Start() ' Remove if you don't want to use timers to display a label temporarily
End If
RealCraze
  • 1
  • 2
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 07 '22 at 00:16