automated testing – Selenium – cannot click on radio button

In the screenshot the HTML code isn’t right. There are a few issues with the tags in the HTML.

I tried to replicate it on my local HTML file (as shared below) and the click works fine.

My HTML:

<html>

<body>
    <span radiogroup class="slds-radio">
        <input radiogroup type="radio" name="id_select" id="radio-1-1-61" value="No">
        <label radiogroup for="radio-1-1-61" class="slda-radio__label">No</label>
    </span>
    <span radiogroup class="slds-radio_faux"></span>
</body>
</html>

MY Selenium Code (with Java. You can translate it to Python):

public static void main(String[] args) {
        try {
            WebDriver driver;
            System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "//chromedriver");
            driver = new ChromeDriver();
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            driver.get("file:///home/milin/Downloads/test.html");
            Thread.sleep(2000);
            driver.findElement(By.xpath("//*[@name="id_select"]")).click();

            Thread.sleep(5000);
            driver.close();
            driver.quit();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Addendum: I also checked this for the HTML code that you have shared in the Comment on the question. This too works fine. Here’s my code for it,

HTML:

<html>

<body>
    <span radiogroup="" class="radio">
        <input radiogroup="" type="radio" name="ID_field" id="radio-1-67935789" value="No"> 
        <label radiogroup="" for="radio-1-67935789" class="radio__label"> 
            <span radiogroup="" class="radio_faux"></span> 
            <span radiogroup="" class="slds-form-element__label">No</span> 
        </label> 
    </span>
</body>
</html>

Selenium (Java):

public static void main(String[] args) {
        try {
            WebDriver driver;
            System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "//chromedriver");
            driver = new ChromeDriver();
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            driver.get("file:///home/milin/Downloads/test.html");
            Thread.sleep(2000);
            driver.findElement(By.xpath("//*[@name="ID_field"]")).click();

            Thread.sleep(5000);
            driver.close();
            driver.quit();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Also note; the name attribute for the <input> tag in your screenshot and the HTML code you have shared is different.

In the screenshot it is name="id_select" and in the HTML code you shared it is name="ID_field".


Addendum 2: Adding alternative code that works after OP commented that there is also radio button with same name.

This time I used the CSS Selector instead of the Xpath to select/click the radio button.

My HTML:

<html>

<body>
    <span radiogroup="" class="radio">
        <input radiogroup="" type="radio" name="ID_field" id="radio-1-67935789" value="No"> 
        <label radiogroup="" for="radio-1-67935789" class="radio__label"> 
            <span radiogroup="" class="radio_faux"></span> 
            <span radiogroup="" class="slds-form-element__label">No</span> 
        </label> 
    </span>
    <span radiogroup="" class="radio">
        <input radiogroup="" type="radio" name="ID_field" id="radio-1-67935789" value="Yes"> 
        <label radiogroup="" for="radio-1-67935789" class="radio__label"> 
            <span radiogroup="" class="radio_faux"></span> 
            <span radiogroup="" class="slds-form-element__label">Yes</span> 
        </label> 
    </span>
</body>
</html>

My Selenium (with Java):

public static void main(String[] args) {
    try {
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "//chromedriver");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get("file:///home/milin/eclipse-workspace/ChannelErrorReports/demo/test.html");
        Thread.sleep(2000);
        driver.findElement(By.cssSelector("input[value="No"]")).click();
        
        Thread.sleep(5000);
        driver.close();
        driver.quit();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Leave a Comment