简体   繁体   中英

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.

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']

.isSelected() function will returns you a boolean value True or False , depending on that you can check the condition and enable or leave the radio button selected.

driver.findElement(By.cssSelector("input[id='26110162']")).isSelected().

Declare a boolean value and store the result then provide an if condition to validate

You can try any of this method:-

  1. selenium.isChecked(Locator);

  2. List<WebElement> radio = driver.findElements(By.name("address")); radio.get(0).getAttribute("checked"));

Hope it will help...

Try in selenium java:

String name = driver.findElement(By.xpath("path"));

assertEquals(name.isSelected(),true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM