I'm trying to select a checkbox attribute in xpath which is named "checked type".
I've tried //[#checked type], //[#checked-type], //[#checked_type], and //[#checked\stype] but none of them appear to be selecting any elements, while //*[#type] is working fine.
Is it possible to select an attribute with spaces?
checked and type are 2 attributes but not one, that's the reason why you are not able select the element using //[#checked type], //[#checked-type], //[#checked_type], and //[#checked\stype] locators.
You can define the checkbox status using checked attribute as shown in the below sample html.
<!DOCTYPE html>
<html>
<body>
<div>
<label> Checkbox Checked - Using checked attribute value</label>
<input checked='checked' type='checkbox'></input>
</div>
<div>
<label> Checked - simple approach</label>
<input checked type='checkbox' ></input>
</div>
<div>
<label> No Checked attribut</label>
<input type='checkbox'></input>
</div>
<div>
<label> Radio Checked - Using checked attribute value</label>
<input checked='checked' type='radio'></input>
</div>
<div>
<label> Radio Not Checked - No checked attribute value</label>
<input type='radio'></input>
</div>
</body>
</html>
If you want to set the checkbox/radio 'selected' by default then you can use either checked without attribute value or checked='checked'.
Refer to checked attribute in this page for more information.
So now, let's see how you can select the element.
Checkbox
//input[#checked][#type='checkbox']
//input[#checked='checked'][#type='checkbox']
Radio:
//input[#checked][#type='radio']
//input[#checked='checked'][#type='radio']
Related
Here is my code :
${verifications} Create List
Set Test Variable ${verifications}
Input text ${societes_input_nom} ${nom_etablissement} ${TEST_NAME}
Append To List ${verifications} ${nom_etablissement} ${TEST_NAME}
Input text ${societes_input_contact_email_casino} ${email}
Append To List ${verifications} ${email}
Input text ${societes_input_contact_adresse} ${adresse}
Append To List ${verifications} ${adresse}
Input text ${societes_input_site_web} ${url_siteweb}
Append To List ${verifications} ${url_siteweb}
FOR ${item} IN #{verifications}
Page Should Contain ${item}
END
Here the HTML for the input :
<div class="form-group">
<label for="title"><h4>Nom de l'établissement
</h4></label>
<br>
<div class="input-group">
<div class="input-group-addon" >
<i class="fa fa-building">
</i>
</div>
<input type="text" style="display: none;" id="initialvalue_prop_1" name="initialvalue_prop_1" maxlength="1000" value="TNR Activite Casino"
/>
<input type="text" id="prop_1" name="prop_1" maxlength="1000" value="TNR Activite Casino"
/>
But, the "page should Contain" do not find my ${nom_etablissement} ${TEST_NAME}
But when i'm looking the page, thoses words are in the page (and the DOM)
The error :
Page should have contained text 'TNR Activite Casino' but did not.
I don't know how to fix that, I tried many thing but nothing work...
Can you help me pls ?
Page Should Contain check for actual text and not the values of the attribute. To get value of attribute you should use -
${Value}= Get Element Attribute xpath://input[#id='initialvalue_prop_1'] value
If you want to assert value inside the textfield then use the keyword -
Textfield Value Should Be ${textfield_locator} ${value_to_be_asserted}
For more details Get Element Attribute
I am building an app using Flask to show nearby shops, and the user can like a shop so it can be added to their liked shops list.
My question is how can i get the value inside the <h4> tag sent to Python knowing that the name attribute is a variable. The html code is below:
<form name='likeF' method='POST' action ='{{url_for("liked")}}'>
<h4 name="ShopName_{{loop.index}}">{{item.ShopName}}</h4>
<p>
Shop Description : {{item.ShopDesc}} <br>
<button type="submit" class="success button like">Like</button>
distance: {{item.ShopDistance}}
</p>
</form>
So please how am I supposed to get back the value of {{item.ShopName}}?
You can't send the text inside an <h4> tag (or any tag that is not a form input) back to the server, at least not without using Javascript. The easiest method would be to duplicate it in a hidden input element inside the form, such as:
<input type="hidden" name="ShopName" value="{{ item.ShopName }}">
Then you can access it using request.form["ShopName"].
If I had an html code with:
<a name="patient_name" href="{{url_for('patient_history')}}">James</a>
Is it possible for me to get this value of 'James' via something like
request.form['patient_name']?
Is there any similar way that the value of James can be passed to the back end?
There are 2 ways to submit value to the backend:
using GET: like Klaus D. has mentioned, you can put it at the href:
<a name="patient_name" href="{{url_for('patient_history')}}/?value=James">James</a>
using POST: by using form tag and hidden input field:
<form action={{url_for('patient_history')}} method="POST">
<input type="text" value="James" hidden="hidden" />
<button name="patient_name" type="submit">James</button>
</form>
Q: What XPath or CSS selector I can use to select 2nd <div class="checkbox">?
I have tried to use:
XPath - //div[#class="checkbox"][2]
CSS - div.checkbox:nth-child(2)
However none of them worked on chrome developer tool.
I can use $x('//div[#class="checkbox"]') to see all three checkboxes
I can use $x('//div[#class="checkbox"]')[0] to specify the 1st div.checkbox
I can use $x('//div[#class="checkbox"]')[1] to specify the 2nd div.checkbox
Here's an example of my HTML Structure
<div class="fs">
<div class="f">
<div class="checkbox">
<input type="radio" value="A">
<label for="A">A</label>
</div>
</div>
<div class="f">
<div class="checkbox">
<input type="radio" value="B">
<label for="B">B</label>
</div>
</div>
<div class="f">
<div class="checkbox">
<input type="radio" value="C">
<label for="C">C</label>
</div>
</div>
</div>
Rather than trying to find the second element by index, another possibility would be to get it by the value on the INPUT or the text in the LABEL that is contained in that DIV. A couple XPaths would be
//div[#class='checkbox'][./input[#value='B']]
//div[#class='checkbox'][./label[.='B']]
You need 2nd element from the results. Which can be done by using below
(//div[#class="checkbox"])[2]
I think CSS doesn't allow such a thing to select from a result
Since JeffC and Tarun Lalwani already suggested XPath way of doing it, I'd like to suggest a different approach.
In CSS, one can use :nth-child selector to choose 2nd <div class="f"> and grab the nested div from there. (> can be omitted)
div.f:nth-child(2) > div.checkbox
Similarly, the following works in XPath:
//div[#class='f'][2]/div[#class='checkbox']
One can choose an element based on the attribute value with CSS selector using Attribute selectors, but one cannot select the parent, unfortunately.
I'm using Bootstrap with Flask Python.
request.form.get("name")
#name is the name of the form element(checkbox)
<label class="btn btn-danger pzt active">
<input type="checkbox" name="name" value="1" data-id="0"> Check
</label>
When checkbox is checked, parent label has class "active", I want to get if checked box is checked. Is there any way or methods?
you can try the following:
HTML:
<div class="checkbox">
<label>
<input type="checkbox" name="check" value="edit"> New entry
</label>
</div>
In flask:
value = request.form.getlist('check')
This will give you the value of the the checkbox. Here value will be a list.
value = [u'edit']
You can also get value of multiple checkboxes with same name attribute.
I'm not familiar with Flask, but I do know how HTTP works.
If you want to know if its checked on the server side, just check if that form field exists, if request.form.get("name") gives you NULL or exception, then the checkbox should be unchecked.
If you want to know it on the client side with javascript, you can use jQuery (as jQuery is a base component of Bootstrap) as $('xxxx').is(':checked') (replace xxxx with a valid selector).