Koushic
Koushic

Reputation: 153

XPATH- How to select radio button and text in a single xpath expression?

I have Radio button with value as "CREDIT_CARD" and the text of the radio button as "New". Now i need to select a radio button which has text "New".

<div>
<input type="radio" onchange="javascript:toggleAdvancedDisplay('pay_detail','CREDIT_CARD');" value="CREDIT_CARD" name="payment_type" style="margin:0; vertical-align: middle;"/>
<span class="value">New</span>

I tried the below xpath, but it doesn't locate the expected element.

/fieldset[1]/div/div/div[2]/input[@value='CREDIT_CARD']/fieldset[1]/div/div/div[2]/span[contains(text(), 'New')]

What is it i am doing wrong here?

Upvotes: 1

Views: 10230

Answers (3)

Django_Tester
Django_Tester

Reputation: 139

Try this. Does this locate the element you're looking for?

xpath = //*[@value="CREDIT_CARD"]/span[contains(.,"New")]

Upvotes: 0

Amith
Amith

Reputation: 7008

You can try :

xpath = //input[@value='CREDIT_CARD' and following-sibling::span[contains(., 'New')]]

This will get input tag with CREDIT_CARD as value and whose sibling contains New as text.

Upvotes: 3

Mark Veenstra
Mark Veenstra

Reputation: 4739

The <span> element is not a child of the <input> element, but it is the next sibling. XPath should be:

/fieldset[1]/div/div/div[2]/input[@value='CREDIT_CARD' and following-sibling::span[1] = 'New']

Upvotes: 2

Related Questions