'How do I programmatically click "Submit" on a Google form using Visual Basic?

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>


Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RealCraze