I'm trying to use xPath to click on the yes checkbox but I can't seem to figure it out. I have tried
driver.find_element_by_xpath(".//label[following-sibling::input[contains(., 'Yes')]]").click() .
but this didn't work.
In the past I have used
driver.find_element_by_xpath("//input[#id=(//label[text()=\"Phone Number\"]/#for)]")
to find text fields but I haven't been able to do the same for the checkbox.
Any help would be much appreciated.
<dl data-validation="{"checkboxes":
{"required":true,"and":
[{"name":"checkboxes","operator":"checkboxes","options":{"message":"must select a value"}}]}}" class="form">
<dt><div class="form_response_label required" id="responses_2865807_label">Are you an Undergraduate Student?</div></dt>
<input id="responses_2865807_" name="responses[2865807][]" type="hidden">
<div class="form-checkbox">
<label for="responses_2865807_4737821">
<input id="responses_2865807_4737821" name="responses[2865807][]" type="checkbox" value="4737821">
Yes
</label> </div>
<div class="form-checkbox">
<label for="responses_2865807_4737822">
<input id="responses_2865807_4737822" name="responses[2865807][]" type="checkbox" value="4737822">
No
</label> </div>
<small><span class="right char_count"></span></small><div class="form-constraints"><div class="validation-summary" style="display: none;"><strong>Validation</strong><div class="explanation">Valid input may include: must select a value</div></div></div></dl>
My main focus is checking yes in the following code:
<label for="responses_2865807_4737821">
<input id="responses_2865807_4737821" name="responses[2865807][]" type="checkbox" value="4737821">
"
Yes
"
</label>
Now that I figured out how to find the check that says "Yes", how would I be able to select "Yes" specifying the label that came before it, in case I get into a situation where i need to change between Yes and No depending on the question?
So in the example code above I would clarify that I am looking for the "Yes" checkbox labeled by "Are you an Undergraduate Student?"
Found the answer! Just needed to add .. instead of .
driver.find_element_by_xpath("//label/input[contains(..,'Yes')]").click()
Related
<li>
<input type="radio" id="financial_data_0"
name="financial_d" data-dependson="[]" value="1">
<label for="financial_data_0">
::before
" Yes "
::after
</label>
<\li>
Hi guys, from this html above, I am trying to select the button "yes", in the following way, but it does not work:
driver.find_element_by_id("financial_data_0").click()
How can I solve this problem?
this is my original screenshot.
And this is the screenshot after I have click the on-off area on it.
This is the original html for the on-off area:
<div region="child-0" class="togglebar-item inline-block region region-child-0">
<div data-view-name="anonymous-view-11401" data-render-count="2">
<div class="ftnt-on-off-switch-ct">
<div class="ftnt-on-off-switch inline-block">
<input type="checkbox" id="ftnt-on-off-input--toggle-0" class="" action="action" data-mkey="1325" data-id="toggle-0">
<label for="ftnt-on-off-input--toggle-0"></label>
<div class="slider"></div>
</div>
</div>
</div>
</div>
And this is the html after the on-off area has been clicked.
<div region="child-0" class="togglebar-item inline-block region region-child-0">
<div data-view-name="anonymous-view-11492" data-render-count="2">
<div class="ftnt-on-off-switch-ct">
<div class="ftnt-on-off-switch inline-block">
<input type="checkbox" id="ftnt-on-off-input--toggle-0" class="" action="action" checked="" data-mkey="1325" data-id="toggle-0">
<label for="ftnt-on-off-input--toggle-0"></label>
<div class="slider"></div>
</div>
</div>
</div>
</div>
My question is:
In selenium python, after I click the on/off area, how may I detect the on/off happens ???
I need to add a test case for this, as sometimes even People have click the on-off, it never happens.
Alternately you may use something like this as well . I have tried this approach in java and you can definitely use it in python as well.
String color = element.getCssValue("color");
You can get the color from the style tab in the inspector and check if the color has changed
# if its checked checkboxes
wait.until(EC.presence_of_element_located((By.cssSelector, "input:checked[type='checkbox']")))
# if its not checked checkboxes
wait.until(EC.presence_of_element_located((By.cssSelector, "input:not(:checked)[type='checkbox']")))
So I normally use driver.find_element_by_xpath('//input[#id=(//*[contains(text(), "User Name")]/#for)]') to enter information into text boxes, but this hasn't been working for the following code:
<div class="form-group text-entry required ">
<div class="label-set" id="answerTextBox-30918642">Primary User Name (First and Last Name)</div>
<div class="group-set" role="group" aria-labelledby="answerTextBox-30918642">
<input id="pageResponse_Responses_Index" name="pageResponse.Responses.Index" type="hidden" value="fta30918642">
<input class="freeTextAnswerId form-control" id="pageResponse_Responses_fta30918642__FreeTextAnswerId" name="pageResponse.Responses[fta30918642].FreeTextAnswerId" type="hidden" value="30918642">
<label for="answerTextBox-30918642-free" class="sr-only">Write-In Answer</label>
<input class="form-control free-text" id="answerTextBox-30918642-free" name="pageResponse.Responses[fta30918642].FreeText" type="text" value="">
</div>
</div>
I tried messing with the xpath to select the input,
driver.find_element_by_xpath("//label[contains(.,'User Name')]/following-sibling::input[1]")
but so far nothing I've tried so far has worked correctly. This works to find the element containing the label, driver.find_element_by_xpath("//*[contains(text(), 'User Name')]"), but my issue is then selecting the input to send keys to.
You can simplify your XPath to
//div[contains(.,'User Name')]/following::input[#type='text']
The following axis from MDN.
The following axis indicates all the nodes that appear after the context node, except any descendant, attribute, and namespace nodes.
The other INPUTs are hidden so specifying #type='text' will find only the one you want.
So I was able to get it working integration Santhosh's suggestion:
//*[contains(text(), \"User Name\")]/../div//input[#class='form-control free-text']
I am trying to write a code that I can use to automate a training course that I have to do every year. It is the same material and the same training year after year, so I figured why not automate it.
The part that I am stuck is clicking on a radio button. I am able to select the radio button through using find element by xpath, but because the answers are randomized I would like to find the element by the text related to the radio button. I have tried using find_element_by_partial_link and had no luck, I could also being doing it wrong. This is what I have tried:
test = browser.find_element_by_partial_link_text('Is this achievable?').
Here is the element that I am trying to access:
<label for="q1789110:1_answer0" style="background-color: rgb(234, 114, 0);" id="yui_3_17_2_3_1509578998475_118">Is this achievable?</label>
Any help will be greatly appreciated. Thank You.
If your html code look like this
<form action="/action_page.php">
<label for="male">Male</label>
<input type="radio" name="gender" id="male" value="male"><br>
<label for="female">Female</label>
<input type="radio" name="gender" id="female" value="female"><br>
<label for="other">Other</label>
<input type="radio" name="gender" id="other" value="other"><br><br>
<input type="submit" value="Submit">
</form>
You can first use xpath in order to find label node using text and then get it's 'for' attribute:
for_attr = driver.find_element_by_xpath("//label[text()='Male']").get_attribute("for")
Then you can find the input element in order to click on it:
By xpath
driver.find_element_by_xpath("//input[#type='radio' and #id='%s']" % (for_attr)).click()
or by id
driver.find_element_by_id(for_attr).click()
The for attribute of the label tag should be equal to the id attribute of the related element to bind them together.
If this is your element:
<script>
function myFunction() {
alert("Hello!")
}
</script>
<label for="q1789110:1_answer0" style="background-color: rgb(234, 114, 0);" id="yui_3_17_2_3_1509578998475_118" onclick="myFunction()">Is this achievable?</label>
and you want to use the xpath, you can use the and condition with "text()":
driver.find_element_by_xpath("//label[#id='yui_3_17_2_3_1509578998475_118' and text()='Is this achievable?']").click()
EDIT
If the id changes, you can just check the text value:
driver.find_element_by_xpath("//label[text()='Is this achievable?']").click()
I'm using Django 1.8 and python 2.7
In my template I have a radio button.
<div class="form-group">
<label class="col-md-4 control-label" for="radios">Liquids:</label>
<div class="col-md-4">
<label class="radio-inline" for="radios">
<input type="radio" name="liquids" id="liquids" value="1" disabled>
With cough
</label>
<label class="radio-inline" for="radios">
<input type="radio" name="liquids" id="liquids" value="0" >
No cough
</label>
</div>
</div>
When I push the submit button whatever is my choice, the first option is taken as selected. Do you know how to fix this?
How do I get the correct value of radio choice in views.py?
Also, when I render the template having filled values with selected the 2nd option, the radio button for the first option is selected in template.
Can you provide your complete HTML page? Probably you forgot to use <form></form> HTML tag.
When you need to get the selected values of an input element you can use jQuery. The function .serializeArray(); will come in handy.
var fields = $( ":input" ).serializeArray();