2

I have this HTML

<div class="Radio">
 <label>
  <input class id="checkbox" name="category" type="radio" value="1">
  <strong> TONY STARK </strong>
 </label>

 <label>
  <input class id="checkbox" name="category" type="radio" value="2">
  <strong> IRON MAN </strong>
 </label>

 <label>
  <input class id="checkbox" name="category" type="radio" value="3">
  <strong> ROBERT DOWNEY </strong>
 </label>

I need to select radio buttons based on TONY STARK , IRON MAN , ROBERT DOWNEY as the user passes it as a flexible parameter

I tried this, but any other easy way would definitely help me!

driver.FindElement(By.Id("checkbox"));
for(WebElement radiobutton: radiobuttons)
{ 
  if(radiobutton.getAttribute("value").equals("TONY STARK"))
  radiobutton.click();
}

5 Answers 5

2

You should try using Xpath to get single radio button and avoid looping as below :-

string Xpath = ".//input[following-sibling::strong[contains(.,'TONY STARK')]]";

Or

string Xpath = ".//label[contains(.,'TONY STARK')]/input";

Use any one of these Xpath to find radio button as :

var radio = driver.FindElement(By.Xpath(Xpath));
radio.Click();
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, That worked out! So, @Saurabh, how do i find the text by value ?
I'm sorry but I didn't understand what are you asking?? By which value do you want to find text??
2

You can use xpath to do that. Try the below xpath .//label[contains(text(),'TONY STARK')]/input[@id='checbox']

Comments

2

Create a class to wrap up radios from now on:

public class RadioButtons
{
    public RadioButtons(IWebDriver driver, ReadOnlyCollection<IWebElement> webElements)
    { 
        Driver = driver;
        WebElements = webElements;
    }

    protected IWebDriver Driver { get; }

    protected ReadOnlyCollection<IWebElement> WebElements { get; }

    public void SelectValue(String value)
    {
        WebElements.Single(we => we.GetAttribute("value") == value).Click();
    }
}

Then you can use it like this:

RadioButtons categories = new RadioButtons(Driver, driver.FindElements(By.Name("category")));

categories.SelectValue("TONY STARK");

Comments

2

I have this other solution:

public void RadioButtonClickByNameAndValue(string name, string val)
        {
            ScreenComponent.Wait.WaitVisibleElement(By.Name(name));
            ScreenComponent.Script.ExecuteJavaScriptCode($"$('[name={name}][value={val}]').click();");
        }
  • In another class I say what value I want to click and in another class I say what is the element.

  • in my case the radio options value are "True" and "False".

Comments

0

Identify a radio button using XPath with index

IWebElement radioButton = driver.FindElement(By.XPath("//*[@type='radio'][1]");
radioButton.Click();

where:

Asterisk * means wildcard character.

[1] is the position of radio button options ("TONY STARK")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.