How to Click on the Radio Button Through the Element Id Attribute Using Selenium and C#

How to click on the radio button through the element ID attribute using Selenium and C#

To locate the element you can use either of the following Locator Strategies:

  • CssSelector:

    driver.FindElement(By.CssSelector("input#group[value='In_Group']"));
  • XPath:

    driver.FindElement(By.XPath("//input[@id='group' and @value='In_Group']"));

However, as it is a <input> element and possibly you will interact with it ideally you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies:

  • CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.custom-radio#group[value='In_Group'][name='group']"))).Click();
  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@id='group' and @value='In_Group'][@class='custom-radio' and @name='group']"))).Click();

How to click on the radio button with label Yes using Selenium and C#

The radio-button is basically an <input> element associated with the <label> with text as Yes and to click() on it you can use either of the following Locator Strategies:

  • XPath:

    driver.FindElement(By.XPath("//label[contains(., 'Yes')]//ancestor::input[1]")).Click();

Ideally, you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategy:

  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//label[contains(., 'Yes')]//ancestor::input[1]"))).Click();

Using Selenium, how to click a radio button based on associated label

You have two options.

Option 1 - do it all within a single XPath query

The XPath query would be:

//input[@id=//label[text()='TestCheckBox']/@for]

That is, get input that has an id which comes from the for attribute from a label that has the text of "TestCheckBox"

Option 2 - get the attribute from the label and then find the element in seperate steps

public void ClickRadioButtonByLabelText(string labelText)
{
// (1) Find the element that has the label text as its text
IWebElement labelForButton = commondriver.FindElement(By.XPath("//label[text()='labelText']"));

// (2) Get the FOR attribute from that element
string forAttribute = labelForButton.GetAttribute("for");

// (3) Find the input that has an id equal to that FOR attribute
IWebElement radioElement = commondriver.FindElement(By.Id(forAttribute));

// (4) Click that input element
radioElement.Click();
}

Trying to click radio button option by name/value in Selenium using C#

A radio button is not considered a <select> element. it is a <input> element with the attribute type='radio'. Checking and Clicking are both sufficient. (clicking moreso sometimes due to ajax binds, etc)

IWebElement radio = commondriver.FindElement(By.Id(element));
radio.click(); // this will work
radio.check(); // so will this.

select radio button by index

You should try as below :-

  • if you to select radio button by their value try as below using By.XPath() :-

    IWebElement element = driver.FindElement(By.Xpath("//input[@value = '5273786']"));
    element.Click();

    Or try as below using By.Id() :-

      IList<IWebElement> elements = driver.FindElements(By.TagName("select"));
    var element = selectElements.Where(se => se.GetAttribute('value') == '5273786');
    element.Click();
  • if you want to select radio button using index try as below using By.Xpath() :-

     IWebElement element = driver.FindElement(By.Xpath("(//input[@id = 'XXXX'])[1]"));
    element.Click();

    Or try as below using By.Id() :-

    IList<IWebElement> elements = driver.FindElements(By.Id("XXXX"));
    elements[0].Click();

Hope it helps...:)

Selenium-C#: How to click on a button when it is clickable and not having the disabled attribute in Blazor Web Application

Invoke click on the element with text Speichern when the disabled attribute is no more present inducing WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies:

  • CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div.mud-dialog-actions button[type='submit']:not(disabled)"))).Click();
  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[@class='mud-dialog-actions']//button[@type='submit' and not(@disabled)]"))).Click();

Cannot check the radio button in selenium webdriver

Your XPath expression lacks the element you're looking for, and thus is invalid.

I'm guessing you're only searching <input> elements. I also removed the unnecessary string(...) function call.

driver.FindElement(By.XPath("//input[@value='inside']")).Click();

Selenium Webdriver: Click on radio button not working

Try using JavaScript like below:

WebElement radioBtn1 = driver.findElement(By.id("radioButton1"));
((JavascriptExecutor) driver).executeScript("arguments[0].checked = true;", radioBtn1);

If you are using QMetry Automation Framework, you should create custom radio button component like where you can override click method with such custom implementation.

How to click on an list element within ol using Selenium and C#

To click on the <li> element with text Afghanistan you have to induce WebDriverWait for the ElementToBeClickable() and you can use either of the following Locator Strategies:

  • CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div.sc-select[aria-label^='Country of residence']"))).Click();
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("ol.sc-select-show li[id=':2']"))).Click();
  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[@class='sc-select' and starts-with(@aria-label, 'Country of residence')]"))).Click();
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//ol[@class='sc-select-show']//li[text()='Afghanistan']"))).Click();


Related Topics



Leave a reply



Submit