0

I have tried all the possibilities but am unable to click the radio button Yes or No. It would be great if I get some help. I have observed that Need to take this attribute (<span radiogroup class="slds-radio_faux"></span>) to click on radio button

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

I tried this code:

driver.find_element_by_xpath("//*[@name = 'id_select' and @value = 'No']/label/span").click()
4
  • Try driver.find_element_by_xpath("//input[@name = 'id_select']".
    – Mate Mrše
    Commented Jan 21, 2022 at 13:23
  • Tried but getting element unable to locate element
    – Mythri N
    Commented Jan 21, 2022 at 13:27
  • share more of the HTML, because it is hard to tell like this. Copy and paste it, rather than screenshoting.
    – Mate Mrše
    Commented Jan 21, 2022 at 13:47
  • <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>
    – Mythri N
    Commented Jan 21, 2022 at 13:56

1 Answer 1

1

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/eclipse-workspace/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();
    }
3
  • Thanks for the above solution, but even for Yes radio button name field is same. <input radiogroup="" type="radio" name="ID_field" id="radio-1-67935789" value="Yes">
    – Mythri N
    Commented Jan 24, 2022 at 3:58
  • In that case, would you please provide the complete HTML with both radio buttons? Commented Jan 24, 2022 at 4:07
  • In the meanwhile I have added an alternative that works. Commented Jan 24, 2022 at 4:14

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.