0

Below is the code :

I want to select the radio button which is present within the span class. Could you please help in identifying a successful xpath ?

<div id="quicklinkbox" class="col-md-2" data-position="left" data-intro="You     can directly download payslip PDFs for the last 3 or 6 months. (India payslip     only)" data-step="4">
<div style="background-color: transparent; margin-top: 28px;">
<div id="panel-title" ng-click="changeExpand(expand);" style="position: relative; left: 24px; cursor: pointer; font-weight: 500;">Quick Download</div>
<div id="panel-title" class="panel-body" style="text-align: left; font-weight: lighter; padding: 5px 0px 0px 50px; font-weight: 400">
<span style="margin-left: -16px;">Payslips for</span>
<form class="ng-pristine ng-valid">
<div class="form-group">
<div class="radio">
<span same-heightcol="" style="font-size: 16px;">
<input class="ng-pristine ng-valid" type="radio" ng-model="payslipselectedopt" style="cursor: pointer;" value="3"     name="payslipradio"/>
<span style="font-size: 16px; position: relative; top: -5px;">Last 3 months</span>
</span>
</div>
<div class="radio">
<span style="font-size: 16px;">
<input class="ng-pristine ng-valid" type="radio" ng-model="payslipselectedopt" value="6" name="payslipradio" style="cursor: pointer;"/>
<span style="font-size: 16px; position: relative; top: -5px;"> Last 6 months</span>
</span>
</div>
<img style="margin-bottom: -12px; margin-left: -16px; cursor: pointer;" ng-click="downloadbulkpayslip()" src="appResources/images/Download-button.png"/>
</div>
</form>
</div>
</div>
</div>
</div>
Arun Balu
  • 15
  • 6

2 Answers2

1

You can use:

//span/input[@name='payslipradio' and @value='6']

Explanation:

//span -> Will search in all HTML al span tag

/input -> Will searth inside the span tag previous selected a input tag

[@name='payslipradio' and @value='6'] -> Will search in the previous selected tags one that the name attr is equals to 'pslpradio' and value attr equals to '6'

Community
  • 1
  • 1
Striter Alfa
  • 1,577
  • 1
  • 14
  • 31
  • There are 2 radio buttons with same name but different values - one with 6(as shown in above code) and another one with value 3. Sorry, I should hav elaborately told upfront. – Arun Balu Jul 21 '16 at 16:52
  • As `value` is a attr, you can specify it in the xpath, like i did it in name. Somethink like: `//span/input[@name='pslpradio' and @value='6']`. You can change the value to select the radio that you want – Striter Alfa Jul 21 '16 at 18:07
  • It is not locating the element. Unable to locate element: {"method":"xpath","selector":"//span/input[@name='pslpradio' and @value='6']"} – Arun Balu Jul 22 '16 at 08:29
  • The XPath is working fine, i have tested it online with your html code. So if Selenium (or other framework that you're using) is not finding the input, probably it is inside a frame or iframe element. And if it is, you need to swith the frame before find and after return to the base context. Here a example using Java and Selenium how it works: http://stackoverflow.com/a/8230423/5120498 – Striter Alfa Jul 22 '16 at 13:57
  • Could you please check :) ? I have pasted the whole code now. I went through your link - it was helpful but not sure if that is the important part in our question here. – Arun Balu Jul 23 '16 at 05:24
  • @ArunBalu, I noticed that now, in your full HTML code, the input tag name changed to `payslipradio` instead of `pslpradio`, that is in your previous HTML code. So, if you change the XPath to `//span/input[@name='payslipradio' and @value='6']` and if this HTML code is not inside any iframe or frame tag, will work. – Striter Alfa Jul 25 '16 at 14:25
  • You can test the xpath here: http://codebeautify.org/Xpath-Tester Inside the XML box, put your HTML code. And in the XPath textbox, the expression. Also you can test it in Chrome Console, with using `$x("your xpath code")` – Striter Alfa Jul 25 '16 at 14:29
  • Thanks a lot. It works, as if it never had a problem now. Previously I was able to identify the path (with my code) for every 2times/5 runs. But now, I am able to locate it without any hitches 3/3 times !! :) Thanks for your detailed explanation and help. – Arun Balu Jul 28 '16 at 00:13
  • You're welcome. If there is no more doubts, please, check your question as resolved. – Striter Alfa Jul 28 '16 at 13:56
0
//*span[input@class='ng-pristine ng-valid' and @type='radio']))