Eular
Eular

Reputation: 1817

python autofill form in a webpage

I am trying to fill a form in a webpage that has a single text box and a send button the html looks like this

<form class="form-horizontal">
                        <div class="row">
                            <div class="col-md-12">
                                <div id="TextContainer" class="textarea-container">
                                    <textarea id="Text" rows="5" maxlength="700" class="form-control remove-border" style="background:none;"></textarea>
                                </div><button id="Send" class="btn btn-primary-outline" type="button" onclick="SendMessage()" style="margin-top:10px" data-loading-text="Loading..."><span class="icon icon-pencil"></span> Send</button>
                            </div>
                        </div>
                    </form>

I tried to use mechanize to submit the form with this code

import re
from mechanize import Browser

br = Browser()
response=br.open("https://abcd.com/")
for f in br.forms():
    if f.attrs['class'] == 'form-horizontal':
        br.form = f
text = br.form.find_control(id="Text")
text.value = "something"
br.submit()

The code runs without an error, but no submission is happening , how do I do it?

Here is the SendMessage function

function SendMessage() {
    var text = $('#Text').val();
    var userId = $('#RecipientId').val();
    if (text.trim() === "")
    {
        $('#TextContainer').css('border-color', 'red');
    }
    else if (new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?").test(text))
    {
        $('#TextContainer').css('border-color', 'red');
        $('#message').html("Links are not allowed in messages");
    }
    else
    {
        $('#Send').button('loading');
        $.ajax(
        {
            url: '/Messages/SendMessage',
            type: 'POST',
            cache: false,
            data:
            {
                __RequestVerificationToken: $('<input name="__RequestVerificationToken" type="hidden" value="CfDJ8MQSRebrM95Pv2f7WNJmKQWGnVR66zie_VVqFsquOCZLDuYRRBPP1yzk_755VDntlD3u0L3P-YYR0-Aqqh1qIjd09HrBg8GNiN_AU48MMlrOtUKDyJyYCJrD918coQPG0dmgkLR3W85gV6P4zObdEMw" />').attr('value'),
                userId: userId,
                text: text
            }

        });
    }

}

Upvotes: 0

Views: 6459

Answers (2)

John
John

Reputation: 1092

You can give name attribute to your text area like:

<form class="form-horizontal">
                        <div class="row">
                            <div class="col-md-12">
                                <div id="TextContainer" class="textarea-container">
                                    <textarea id="Text" name="sometext" rows="5" maxlength="700" class="form-control remove-border" style="background:none;"></textarea>
                                </div><button id="Send" class="btn btn-primary-outline" type="button" onclick="SendMessage()" style="margin-top:10px" data-loading-text="Loading..."><span class="icon icon-pencil"></span> Send</button>
                            </div>
                        </div>
                    </form>

Then try this out:

import re
from mechanize import Browser

br = mechanize.Browser()
br.open("https://abcd.com/")
br.select_form(nr=0) #in case of just single form you can select form passing nr=0
br["sometext"] = "something"
response  = br.submit()
print(response.read())

If it successfully submits form then you can read your response body.

Upvotes: 0

m_____z
m_____z

Reputation: 1591

I suspect the issue is that the submit button in the HTML form is not of type=submit - so mechanise won't know what to do when you call br.submit(). The fix is to either change the button type on the HTML website, or tell Browser which button to use for submitting the form:

br.submit(type='button', id='Send')

The submit method takes the same arguments as the HTML Forms API, so I recommend taking a look at the documentation for more details.

Update

The problem here seems to be the JavaScript method attached to the button. Mechanize does not support calling JavaScript functions, hence you won't be able to just use the .submit() method to submit the form. Instead, the best option would probably be to read in the SendMessage() JavaScript function, which gets called if someone clicks on the Send button, and translate it to Python manually. In the best case it consists of a simple AJAX POST request which is very easy to implement in Python. Please look here for a related question.

Second Update

Given the new information in your question, in particular the JavaScript function, you can now manually implement the POST request inside your Python script. I suggest the use of the Requests module which will make the implementation much easier.

import requests

data = {
    "__RequestVerificationToken": "CfDJ8MQSRebrM95Pv2f7WNJmKQWGnVR66zie_VVqFsquOCZLDuYRRBPP1yzk_755VDntlD3u0L3P-YYR0-Aqqh1qIjd09HrBg8GNiN_AU48MMlrOtUKDyJyYCJrD918coQPG0dmgkLR3W85gV6P4zObdEMw",
    "userId": "something",
    "text": "something else"
}

response = requests.post("https://example.com/Messages/SendMessage", data=data)

response will now consist of the response which you can use to check if the request was successfully made. Please note that you might need to read out the __RequestVerificationToken with mechanize as I suspect it is generated each time you open the website. You could just read out the HTML source with html_source = br.read() and then search for __RequestVerificationToken and try to extract the corresponding value.

Upvotes: 1

Related Questions