Python Mechanize change unnamed input value (known id) - python

as is written in topic i have to change value of some input field using mechanize but i dont have name of it only id :/ Let's stick to the point.
This is how form looks:
<form id="Login" name="Login">
<div id="login-inputs-div">
<input id="Username" type="hidden" name="username"></input>
<input id="Password" type="hidden" name="password"></input>
<input id="PopupUsername" class="input-text input-text-gray" type="text" disabled="disabled" value="admin" maxlength="32" style="width:100px;"></input>
<input id="PopupPassword" class="input-text input-text-gray " type="password" maxlength="32" value="" style="width:100px;" placeholder="hasło"></input>
<input id="bt_authenticate" class="input-btn input-btn-orange translation Translations.general.btn.authenticate translated" type="submit" value="zaloguj"></input>
</div>
<div id="logout-link-div" style="display: none;"></div>
</form>
What i have to do? Fill PopupPassword using some value and later submit it?
My approach looks like:
import mechanize
url = "http://192.168.1.1"
br = mechanize.Browser()
br.set_handle_robots(False)
br.open(url)
br.select_form(name="Login")
br.form().find_control(id="PopupPassword").__setattr__("value", "something")
res = br.submit()
content = res.read()
Im looking forward for some solution, thanks in advance.

find_control() is exactly what you need here. Just don't call br.form, it is not a function:
password_field = br.form.find_control(id="PopupPassword")
password_field.value = "something"

Related

automate filling in web forms

There is a text box (ID) on the website and I want to put 10,000 data in it. After placing each data in this text box, results are displayed in other text boxes according to the ID.
Textbox ID :
<input class="form-control ltr left text-box single-line" data-val="true" data-val-regex="It is 10 numbers" data-val-regex-pattern="^[0-9]{10}$" data-val-required="*required" id="NId" maxlength="10" name="NId" onblur="LoadInfo()" type="text" value="">
Textobx Name (fill after enter the ID in text box) :
<form action="/Employees/Manager/SavePerson" data-ajax="true" data-ajax-begin="onpostcreatebegin" data-ajax-complete="onpostcreatecomplete" data-ajax-loading="#ajaxloading" data-ajax-method="post" data-ajax-mode="replace" data-ajax-update="#result" id="form0" method="post" novalidate="novalidate"><input name="__RequestVerificationToken" type="hidden" value="asdxBadsP7CpS53654as6dadH3865asdadKhjasdad"> <input type="hidden" name="empId" id="empid" value="0">
<div class="panel panel-info">
<div class="panel-body">
<input data-val="true" data-val-regex="It is 10 numbers" data-val-regex-pattern="^[0-9]{10}$" data-val-required="*required" id="NId" name="NId" type="hidden" value="1234567890">
<input data-val="true" data-val-number="The field PersonId must be a number." data-val-required="The PersonId field is required." id="PersonId" name="PersonId" type="hidden" value="254102232">
<input data-val="true" data-val-required="The GuidId field is required." id="GuidId" name="GuidId" type="hidden" value="665xs6asd-cxc2-wq56-8888-30654998b166">
<div class="form-group">
<div class="col-md-4">
<input class="form-control text-box single-line" data-val="true" data-val-regex="enter the name." data-val-regex-pattern="^[\u0600-\u06ff\s]+$|[\u0750-\u077f\s]+$|[\ufb50-\ufc3f\s]+$|[\ufe70-\ufefc\s]+$|[\u06cc\s]+$|[\u067e\s]+$|[\u06af\s]$|[\u0691\s]+$|^$" data-val-required="*required" id="Firstname" name="Firstname" type="text" value="jack">
<span class="field-validation-valid text-danger" data-valmsg-for="Firstname" data-valmsg-replace="true"></span>
</div>
</div>
</div>
</div>
</form>
The page will not reload after entering the value in the text box ID. Shows the value on that page. What solution do you suggest? I do not know which method to consider that is the most optimal method. Can you introduce the most optimal and practical method?
Depending on how complex the task is, you can use either Selenium (Selenium is a script-controlled Browser) with Firefox WebDriver, for example.
It is of course also possible to use the request methods GET and POST as a method with more work but much higher performance.

Click button with Python and requests

I need their help with a requests call for click a button with python and request.
The HTML code the form is this:
<form action="someurl/changedata" enctype="multipart/form-data" method="POST">
<p class="message">
Description
</p>
<div class="data-field-grouping" data-field-grouping-name="Passwords">
<div class="data-field" data-field-name="OldPassword">
<label data-required="data-required" for="OldPassword">Old Password</label><input id="OldPassword" maxLength="128" name="OldPassword" required="required" type="password" /></div>
<div class="data-field" data-field-name="Password">
<label data-required="data-required" for="Password">New Password</label><input id="Password" maxLength="128" name="Password" required="required" type="password" /></div>
<div class="data-field" data-field-name="PasswordConfirmation">
<label data-required="data-required" for="PasswordConfirmation">Confirm Password</label><input id="PasswordConfirmation" maxLength="128" name="PasswordConfirmation" required="required" type="password" /></div>
</div>
<input name="__RequestVerificationToken" type="hidden" value="xwvDA9Y-bzAY4Z9F3UVSnuFlYEuVfD2F8kYY4aD__wKzjQct7y6JZ4Jd6_YpxhQIXq0zcRJ-RfLxleHgT49P-lMecIB55LEyXCMylaTxK1KzI_HqbWM101FGmaK33Y2z0" /><button type="submit">Change Password</button></form>
I’ve been trying with this code and the login works successfully but the next part not, well. It doesn’t gives me error, but just gives me the html of the body.
import requests
from requests_ntlm import HttpNtlmAuth
info = {'OldPassword':'firstpass',
'Password':newpass,
'PasswordConfirmation': newpass,
'__RequestVerificationToken':'0'
}
s = requests.session()
with requests.session() as s:
responsee = s.get("url",auth=HttpNtlmAuth('username', 'firstpass'))
print (responsee.text)
if responsee.status_code == 200:
print("Login successfully")
response = s.post("url2",params=info)
print(response.text)

pyFlask is putting my inputs in my url browser

Briefly, python Flask is the workbench of web hosting I use, and I am trying to create an input form that doesn't appear in your history.
This is my form html:
<form name="ViewWindow" action="/home/ViewWindow/ViewWindowResult/">
<input name="url" type="url" required="required" placeholder="URL Here">
<input type="submit" value="Go">
</form>
And this is the python code working with the input url:
#web_site.route('/home/ViewWindow/ViewWindowResult/', methods=('GET', 'POST'))
def ViewWindowResult():
urlboi = request.values.get('url')
response = urllibrequest.urlopen(url) # import urllib.request as urllibrequest
htmlBytes = response.read()
htmlstr = htmlBytes.decode("utf8")
return html("ViewWindowResult.html", value=htmlstr)
My goal is to get here; /home/ViewWindow/ViewWindow/ViewWindowResult/,
but I end up getting here when I input "https://www.w3schools.com/tags/"; /home/ViewWindow/ViewWindowResult/?url=https%3A%2F%2Fwww.w3schools.com%2Ftags%2F
Why does Flask put my inputs in the url string? I do not intend to do this anywhere.
Edit: You can check this out by going to https://sm--supermechm500.repl.co/home/ViewWindow/
Try specifying the form method like so:
<form name="ViewWindow" action="/home/ViewWindow/ViewWindowResult/" method="post">
<input name="url" type="url" required="required" placeholder="URL Here">
<input type="submit" value="Go">
</form>
use post method like
<form name="ViewWindow" action="/home/ViewWindow/ViewWindowResult/" method="post">
<input name="url" type="url" required="required" placeholder="URL Here">
<input type="submit" value="Go">
</form
and then you python code is
#web_site.route('/home/ViewWindow/ViewWindowResult/', methods=('GET', 'POST'))
def ViewWindowResult():
input=request.form['url']
#write your code here
return(input)
its working for me it will print the url which same you entered

Login a website using python script redirects at same page

i am trying to login into a website using python script having the 'form' tag like this in the source code.
<form action="trylogin.php" method="post">
<input name="authenticity_token" type="hidden" value="dpsTlD8zutj35FVWJTIqtUZGX67qQ/vab33hpPyYuaU=" />
<input id="user_username" maxlength="20" name="username" placeholder="Username" size="20" type="text" />
<input id="user_password" name="password" placeholder="Password" size="20" type="password" />
<input class="submit themed_bg themed-dark-hover-background" name="commit" type="submit" value="Login" />
</form>
and i am trying the following python code
import requests
import lxml
import lxml.html
s = requests.session()
login = s.get('url')
login_html = lxml.html.fromstring(login.text)
hidden_inputs = login_html.xpath(r'//form//input[#type="hidden"]')
form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs}
form['username'] ='user'
form['password'] ='pass'
form['commit'] = 'Login'
response = s.post('url', data=form)
response.ok
response.url
s.get() is working fine and response.ok also gives 'true'output but url of response is same as of previous page. it seems like it is redirecting the same page. i can't login from python. What should i do Should i use header arguement in s.post()? how to know it? and i have used
form['commit'] = 'Login'
since login is in form of input not button, is it correct?
<input class="submit themed_bg themed-dark-hover-background" name="commit" type="submit" value="Login" />

Friendly URL with get request form

i have this simple html form:
<form action="test/" method="get" accept-charset="utf-8">
<input type="text" name="first" value="" id="first">
<input type="text" name="second" value="" id="second">
<input type="text" name="third" value="" id="third">
<p><input type="submit" value="Continue →"></p>
</form>
when user click submit button, i want to get friendly URL like this:
www.site.com/test/first/second/third
how can i do this?
Where the user ends up after filling out the form and clicking "Continue" depends on what you set the action attribute in your form tag to. You've currently set it to test/.
If you want them to end up at test/<first_val>/<second_val>/<third_val>/, then you can either use some JavaScript (per pythonFoo's answer), or you can redirect in the view that test/ points to, using HttpResponseRedirect:
def test_view(request):
return HttpResponseRedirect('/test/%s/%s/%s/' % request.POST['first'],
request.POST['second'],
request.POST['third])
<input type="text" name="first" value="" id="first">
<input type="text" name="second" value="" id="second">
<input type="text" name="third" value="" id="third">
<p><input type="button" value="Continue →" onclick="submitFriendly();"></p>
<script type="text/javascript">
function submitFriendly() {
window.location.href = window.location.href + document.getElementById('first').value + '/' + document.getElementById('second').value + '/' + document.getElementById('third').value;
}
</script>
This should work. It doesn't check if all inputs are filled.

Categories