M M
M M

Reputation: 99

how to select radio button for angular application

<h5 _ngcontent-c2="" class="card-title">Payment Mode </h5>
<!---->
<div _ngcontent-c2="">
    <div _ngcontent-c2="" class="form-group">
        <input _ngcontent-c2="" name="paymentmode" type="radio" style=""> &nbsp;&nbsp;
        <b _ngcontent-c2="">Credit Card</b>
    </div>
</div>
<div _ngcontent-c2="">
    <div _ngcontent-c2="" class="form-group">
        <input _ngcontent-c2="" name="paymentmode" type="radio"> &nbsp;&nbsp;
        <b _ngcontent-c2="">Debit Card</b>
    </div>
</div>
<div _ngcontent-c2="">
    <div _ngcontent-c2="" class="form-group">
        <input _ngcontent-c2="" name="paymentmode" type="radio"> &nbsp;&nbsp;
        <b _ngcontent-c2="">Net Banking</b>
    </div>
</div>
<div _ngcontent-c2="">
    <div _ngcontent-c2="" class="form-group">
        <input _ngcontent-c2="" name="paymentmode" type="radio"> &nbsp;&nbsp;
        <b _ngcontent-c2="">UPI</b>
    </div>
</div>

For above HTML, i am trying to select radio button based on value of radio button. I am using Protractor NuGet package along with selenium webdriver.

IList<NgWebElement> oCheckBox = ngDriver.FindElements(By.XPath("//input[@name='paymentmode']"));
int Size = oCheckBox.Count;
for (int i = 0; i < Size; i++)
{
    String Value = oCheckBox.ElementAt(i).GetAttribute("value");
    if (Value.Equals("Net Banking"))
    {
        oCheckBox.ElementAt(i).Click();
        break;
    }
}

but IList<NgWebElement> does not contain definition for ElementAt.

Is there any way arount to select radio button based on payment mode?

Upvotes: 0

Views: 960

Answers (4)

supputuri
supputuri

Reputation: 14135

Here is the xpath, almost equivalent to @JeffC. But this will make sure even if some tags added between input and b.

//b[normalize-space(.)='Net Banking']/ancestor::div[@class='form-group']//input[@name='paymentmode' and @type='radio']

Upvotes: 0

M M
M M

Reputation: 99

Thank you @JeffC and @Sudha Velan. After adding reference for Linq(using System.Linq;) and making changes and it is working correctly now.

IList<NgWebElement> Rbtn = ngDriver.FindElements(By.XPath("//input[@name='paymentmode']"));
            IList<NgWebElement> RbtnValue = ngDriver.FindElements(By.XPath("//input[@name='paymentmode']/following::b[1]"));
            int Size = Rbtn.Count;
             for (int i = 0; i < Size; i++)
            {
                String Value = RbtnValue.ElementAt(i).Text;
                if (Value.Equals("UPI"))
                {
                    Rbtn.ElementAt(i).Click();
                    break;
                }
            } 

Upvotes: 0

JeffC
JeffC

Reputation: 25610

You can use an XPath to find the radio button by contained text, "Net Banking"

//input[@name='paymentmode']/following::b[.='Net Banking'][1]

I would put this in a method and pass in the value you are looking for to make it more flexible.

Upvotes: 1

Sudha Velan
Sudha Velan

Reputation: 633

Use xpath ://input[@name='paymentmode']/following::b[1]

Hope the following code will help:

IList<NgWebElement> oCheckBox = ngDriver.FindElements(By.XPath("//input[@name='paymentmode']/following::b[1]"));
        int Size = oCheckBox.Count;
        for (int i = 0; i < Size; i++)
        {
            String Value = oCheckBox.ElementAt(i).getText();
            if (Value.Equals("Net Banking"))
            {
                oCheckBox.ElementAt(i).Click();
                break;
            }
        }

Upvotes: 2

Related Questions