Heisenberg
Heisenberg

Reputation: 495

How to input values into a html form in python

I have a simple html form, like this one

<!DOCTYPE html>
<html>
<body>

<form action="/Login/Authenticate" id="Loginform" method="post">
  <label for="fname">User name:</label><br>
  <input class="Txt_bx password" data-val="true" data-val-required="Enter Password" id="txtPassword" name="Log.password" type="text"><br>
  <label for="lname">Password :</label><br>
  <input class="Txt_bx urserName" data-val="true" data-val-required="Enter Username" id="txtusername" name="Log.username" type="password" value="">
</form>

</body>
</html>

I want to insert some text into the input class Txt_bx password and Txt_bx username without using selenium as it is too slow. Is there any other way I can do this?

Any help would be Appreciated!

Upvotes: 0

Views: 1889

Answers (2)

Tochi Bedford
Tochi Bedford

Reputation: 392

Try Using Requests

import requests

response = requests.post(
    'http://url.com/Login/Authenticate',
    data={
        "Log.password": "password",
        "Log.username": "username"
    }

)

print(response.content) #get the response from whatever server

Don't forget to replace url.com with the actual domain

Upvotes: 1

Shoot
Shoot

Reputation: 77

You could use the requests library for Python, and this page should help to introduce you to POST (as seen by the method tag on the form) requests in the library. You will need to POST to /Login/Authenticate, as it is the action of the form.

In your HTML code, log.Password and log.Username will be the dictionary's keys, and the values will be whatever values you want. For example:

data = {
    "log.Username": "mynewusername",
    "log.Password": "mynewpassword"
}

Upvotes: 2

Related Questions