Prerna Puri
Prerna Puri

Reputation: 1

How to click website submit button on third party website

I want to click a website submit button using my c# window application

<p><input type="submit" value="Send" class="wpcf7-form-control wpcf7-submit"/></p></div>

https://www.devpundit.com/contact/ - check this page to see the submit button It seems like it is calling ajax or javascript call to submit the button. now how to do this.

I have tried the below 2 option but not working

webBrowser2.Document.GetElementById("wpcf7-submit").InvokeMember("click");
HTMLInputElement submit = (HTMLInputElement)pass.all.item("wpcf7-form-control wpcf7-submit", 0);

Upvotes: 0

Views: 621

Answers (1)

slightly drifting
slightly drifting

Reputation: 332

First - you are telling the computer to grab the button by its ID, but instead of using the ID, you are putting the class-name where the ID goes. I inspected the element on that page and did not see an "ID" for that element. So let's use what the website gave us.

https://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp This will give you a list of all the elements that share that class. So instead of

webBrowser2.Document.GetElementById("wpcf7-submit").InvokeMember("click");

please try

Edit: So it turns out there is no GetElementsByClassName in the C# WebBrowser. Another approach instead would be recognizing that the button has a value of "send". The "value" is an attribute, and can be accessed in C#. inspectedElement

You can run a for-loop through the elements in the page and check which input elements have the value "send". It's a bit crude, but it should work.

//Create list of HtmlElement objects
        //Populate list with all elements with tag name 'input'
        HtmlElementCollection elementList = webBrowser2.Document.GetElementsByTagName("input");
        //loop through the items in elementList, match the one with value set to 'send'
        foreach (HtmlElement currentElement in elementList)
        {
            if (currentElement.GetAttribute("value") == "Send")
            {
                currentElement.InvokeMember("click");
            }
        }

Upvotes: 1

Related Questions