Selecting xpath input given label - python

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']

Related

How to use python selenium find_element_by_xpath find out above key word

I am trying to find a string. But it doesn't seem to work.
HTML:
<form name="form1" method="post" action="?cz=del&wbid=7683290543&zjt=aaa&lx=CNAME&xl=%C4%AC%C8%CF&fs=" onSubmit="return b_ifsf('delete?');" id="form1">
<td style="width:120px">
<input type="hidden" name="ip" value="aaa.xxx.com.a.bdydns.com." >
<input type="submit" name="rpt$btnDelete" value="delete" />
</td>
</form>
<form name="form1" method="post" action="?cz=del&wbid=2324242122&zjt=bbb&lx=CNAME&xl=%C4%AC%C8%CF&fs=" onSubmit="return b_ifsf('delete?');" id="form1">
<td style="width:120px">
<input type="hidden" name="ip" value="bbb.xxx.com.a.bdydns.com." >
<input type="submit" name="rpt$btnDelete" value="delete" />
</td>
</form>
<form name="form1" method="post" action="?cz=del&wbid=2324242553&zjt=ccc&lx=CNAME&xl=%C4%AC%C8%CF&fs=" onSubmit="return b_ifsf('delete?');" id="form1">
<td style="width:120px">
<input type="hidden" name="ip" value="ccc.xxx.com.a.bdydns.com." >
<input type="submit" name="rpt$btnDelete" value="delete" />
</td>
</form>
How to find out the key word bbb.xxx.com.a.bdydns.com. and then hit submit to delete it?
#EVNRaja's solution was in the right direction.
To locate the text bbb.xxx.com.a.bdydns.com. then click the associated element with value attribute as delete you can use either of the following solutions:
Using xpath and click():
driver.find_element_by_xpath("//form[#id='form1' and #name='form1']//input[#name='ip' and #value='bbb.xxx.com.a.bdydns.com.']//following::input[1]").click()
Using xpath and submit():
driver.find_element_by_xpath("//form[#id='form1' and #name='form1']//input[#name='ip' and #value='bbb.xxx.com.a.bdydns.com.']//following::input[1]").submit()
The URL you are trying to identify is quoted as hidden element.
The html code you have provided:
<input type="hidden" name="ip" value="bbb.xxx.com.a.bdydns.com." >
All the hidden elements in a browser may have a purpose.
Example:
Consider there is text-field and it doesn't numeric values as input, if end-user enters any numeric values there will be an error code displays next to the text-field.
Here, until we enter a numeric text the error message (text inside html tag element) will be hidden.
In the html code you have shared, the value which you want to inspect was quoted inside input tag and has a type="hidden" name="ip" value="bbb.xxx.com.a.bdydns.com.", we can write a compound xpath as follows:
A example with multiple compound statements:
//input[#type = 'hidden' and #name = 'ip' and contains(#value, 'bbb.xxx.com.a.bdydns.com.')]/following-sibling::input
or
A Simple example:
//input[contains(#value, 'bbb.xxx.com.a.bdydns.com.')]/following-sibling::input
With this xpath code we can directly identify the submit and in the next step you can click the button.
You should be able to use a css selector combination of:
[value='bbb.xxx.com.a.bdydns.com.'] + input
Code:
driver.find_element_by_css_selector("[value='bbb.xxx.com.a.bdydns.com.'] + input").click() #.submit()
The first part is an attribute = value css selector then the "+" is an adjacent sibling combinator, followed by an element selector; saying, find input tag element that is an adjacent sibling element to element with attribute value having value of bbb.xxx.com.a.bdydns.com.

How do I use python and selenium to find the element of a radio button using the text related to the radio button?

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()

Need help in writing XPATH for Selenium with Python

Below is the element in DOM.
<div style="float:left;width: 40px">
<label class="radio inline">
<input id="formData99991:Select" class="sample" data-ng-model="sampletId" size="" name="formData99991Select" sample-value="true" style="" value="1234" checked="checked" type="radio"/>
A
</label>
</div>
I tried with //input[contains(#id,'formData99991:Select') and contains(text(),'A')] but it doesn't work.
There are multiple similar elements on the web page and I need to identify each element with combo of id and text.
You can try with following x-path,
//label[contains(.,'A')]/input[#id="formData99991:Select"]

How to click on checkboxes based on label text

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()

Django1.8 and Python 2.7: Radio button stores the first option whatever is my choice

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();

Categories

Resources