Is there a method to read all the values of a datalist with python?
<form action="/cgi-bin/file.py" method="post">
<div>
<label for="name">
Name
</label>
<input type="text" name="name" list="namelist" id="name">
<datalist id="namelist">
<option value="1">
<option value="2">
<option value="3">
<option value="4">
</datalist>
</div>
with python i am able to read the selected value through the command
import cgi
form = cgi.FieldStorage()
name=form.getvalue("name")
but is there a way to put all the values in a list? ['1','2','3','4']
Thanks
Not unless you can read the original page. The browser isn't sending you all possible values.
liist=[]
with open("filename",'r') as m:
for x in m:
if "value" in x:
final=x[19:20]
liist+=final
print(liist)
Related
I have a list of html input elements as below.
lists=[<input type="hidden" name="csrf_token" value="jZdkrMumEBeXQlUTbOWfInDwNhtVHGSxKyPvaipoAFsYqCgRLJzc">,
<input type="text" class="form-control" id="username" name="username">,
<input type="password" class="form-control" id="password" name="password">,
<input type="submit" value="Login" class="btn btn-primary">]
From these I need to extract the attribute values of name, type, and value
For eg:
Consider the input <input type="hidden" name="csrf_token" value="jZdkrMumEBeXQlUTbOWfInDwNhtVHGSxKyPvaipoAFsYqCgRLJzc">
then I need output as following dictionary format
{'csrf_token':('hidden',"jZdkrMumEBeXQlUTbOWfInDwNhtVHGSxKyPvaipoAFsYqCgRLJzc")}
Could anyone please a guidance to solve this
I recommend you to use the Beautiful Soup Python library (https://pypi.org/project/beautifulsoup4/) to get the HTML content and the values of the elements. There are functions already created for that purpose.
I have Flask application, one of the pages has form whcih send form request when I click on Input Submit button, I would like the input button to be disabled after click. In order to do so, I've written jquery fucntion and attached it to input element.
Everything works perfectly in Edge browser, but when I do test in Chrome, the button becomes disabled, but post request is not sent to server.
jquery script which shows loading gif and blocks submit input button:
function loading() {
if ($('#calendar1').val() && $('#calendar2').val()) {
$("#loading").show();
$("#content").hide();
$(':input[type="submit"]').prop('disabled', true);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="" class="ordinary_form" id="wifi_form">
<label for="calendar1"> Choose time period start: </label>
<input type="date" name="calendar1" id="calendar1" required> <br> <br>
<label for="calendar2"> Choose time period end: </label>
<input type="date" name="calendar2" id="calendar2" required></p>
<label for="period"> Choose step: </label>
<select required name="period" id="period">
<option selected value="0">Day</option>
<option value="1">Week</option>
<option value="2">Month</option>
</select>
<br><br>
<input target="_self" type="submit" value="Send" formtarget="{{url_for('wifi')}}" formmethod="post" onclick="loading();">
</form>
Python code (I guess you do not need it, but let it be here):
#app.route("/wifi", methods=["GET", "POST"])
#flask_login.login_required
def wifi():
# something here
if request.method == 'POST':
start_date = request.form["calendar1"]
end_date = request.form["calendar2"]
period = request.form["period"]
# do some work
return render_template("wifi.html", result_dict=result_dict, wifipic="wifi" + pic_end)
Because you can only do that in the submit event
Disabling the button itself on click of the button will stop the form from being submitted
It also seems that your formtarget="{{url_for('wifi')}}" formmethod="post" should be removed. No need to set that when you have a form already
In any case formtarget is not a URL - you may have meant formaction
$("#wifi_form").on("submit",function() {
if ($('#calendar1').val() && $('#calendar2').val()) {
$("#loading").show();
$("#content").hide();
$(':input[type="submit"]').prop('disabled', true);
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="post" action="{{url_for('wifi')}}" class="ordinary_form" id="wifi_form">
<label for="calendar1"> Choose time period start: </label>
<input type="date" name="calendar1" id="calendar1" required> <br> <br>
<label for="calendar2"> Choose time period end: </label>
<input type="date" name="calendar2" id="calendar2" required></p>
<label for="period"> Choose step: </label>
<select required name="period" id="period">
<option selected value="0">Day</option>
<option value="1">Week</option>
<option value="2">Month</option>
</select>
<br><br>
<input type="submit" value="Send" >
</form>
Following is the HTML code for which I need a unique XPath.
Firebug gives me a xpath like
.//*[#id='service_dlg']/form/p[4]/span/input[1] [For Essential]
.//*[#id='service_dlg']/form/p[4]/span/input[2] [For Enhanced] etc.
I need something like [#name = 'Essential'] so that I need not write multiple xpaths in my code. I want to pass values like essential, enhanced and premium from a function.
<p>
<label style="vertical-align:top">Feature Pack:</label>
<span style="display:inline-block">
<input name="pack" value="Essential" type="radio">
<label class="feature">Essential</label>
<input name="pack" value="Enhanced" type="radio">
<label class="feature">Enhanced</label>
<input name="pack" value="Premium" type="radio">
<label class="feature">Premium</label>
<br>
<input name="featurepack" value="u" type="checkbox">
<label class="feature">URL Filtering</label>
<br>
<input name="featurepack" value="t" type="checkbox">
<label class="feature">Threat Prevention</label>
<br>
<input name="featurepack" value="w" type="checkbox">
<label class="feature">Wildfire</label>
<br>
</span>
try as follows for Enhanced:
//input[#value='Enhanced']
To make it generic and receive value as a function parameter, try as follows:
driver.find_element_by_xpath("//input[#value='%s']" % value) # where value is function parameter, can be Essential, Enhanced or Premium
I tried to used bootstrap-select whith the following example, but in Flask I get only one value and not the maximum two values?
<form class="form-horizontal" action="{{url_for('compare')}}" method="get" role="form">
<div class="input-group">
<span class="input-group-addon">Tests:</span>
<select name='tests' class="selectpicker show-menu-arrow form-control" multiple data-max-options="2" data-live-search="true">
<option value="Test1">Test1</option>
<option value="Test2">Test2</option>
<option value="Test3">Test3</option>
</select>
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Search!</button>
</span>
</div>
</form>
<hr>
The route in Flask is define like below:
#app.route("/compare/", methods=["GET"])
#templated('compare.tmpl')
def compare():
tests = request.args.get('tests')
How is it possible to get maximum two selected values in flask e.g. Test1 and Test2?
request.args is a MultiDict. It provides a method called getlist that will return a list of items for the given key rather than a single value. Update your code to the following.
tests = request.args.getlist('tests')
More information can be found in the werkzeug documentation.
This is the Html form:
<form action='use.py'>
<div><input type='text' name='etc'></div>
<p><input type='submit' value='etc!'></p>
</form>
And this is the python for it
colour = form["colour"].value
The top html form is a text box that users can type something in.
If I were to have a drop down box / radio button form like this:
<form action='etc.py'>
<p>List Type:</p>
<div><input type='radio' name='status' value='bulleted' checked>
Bulleted
</div>
<div><input type='radio' name='status' value='numbered'>
Numbered
</div>
<p>Text style:</p>
<div><input type='checkbox' name='bold'>
<b>Bold</b>
</div>
<div><input type='checkbox' name='italic'>
<i>Italic</i>
</div>
<p>Movies to display:</p>
<div>
<select name='type'>
<option>Only numbers</option>
<option>Only names</option>
</select>
</div>
<p><input type='submit' value='Display'></p>
</form>
How would I write my python? Like this?
only_numbers = form["Only numbers"].value
You can write a Python CGI script, but most Python frameworks are built upon WSGI and have a concept of request/response objects. For example, in Django the value would be available at request.POST['fieldname'].
Using the python CGI module, it is like:
form = cgi.FieldStorage()
# for multiple elements with same name or elements with multiple values
a_list = form.getlist("multiple_select_input")
# For single value elements
if "single_value_input" in form:
x = form["single_value_input"].value
else:
x = "default value"
# or
x = form.getvalue("single_value_input", "default value")