user3538483 user3538483 - 3 months ago 7
HTML Question

How to check if the radio button is selected or not in Selenium WebDriver?

Here is my HTML code

<div class="selectCard_left">
<input id="26110162" class="default_shipping_address" type="radio" name="address" checked="true">
<span>Seleccionar como tarjeta predeterminada</span>


I am trying with
driver.findElement(By.id("17390233")).isSelected();
, but I am not getting any value.

Answer
driver.findElement(By.id("26110162")).isSelected();

or

String str = driver.findElement(By.id("26110162")).getAttribute("checked");
if (str.equalsIgnoreCase("true"))
{
    System.out.println("Checkbox selected");
}

if the ID is changing... use the following XPATH:

//input[span='Seleccionar como tarjeta predeterminada']

or

//input[@name='address' and @type='radio']
Comments