nikhil naphade
nikhil naphade

Reputation: 9

How to click on a radio button as per the given HTML

I am using a reference, shown below, for the radio button.

<input id="checkmo" class="radio" type="radio" data-bind="attr: {'id':getCode()}, value: getCode(), checked: isChecked, click: selectPaymentMethod, visible: isRadioButtonVisible()" name="payment[method]" value="checkmo"/>

I have used the below code for this but it failed to click on radio button:

WebElement radioBtn = driver.findElement(By.id("checkmo"));
radioBtn.click();

Upvotes: 0

Views: 139

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193078

The Radio Button with which you are trying to interact is a React element so you need to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.radio#checkmo"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='radio' and @id='checkmo']"))).click();
    

Upvotes: 1

cruisepandey
cruisepandey

Reputation: 29362

You may introduce the WebDriverWait which is explicit wait in Selenium.

Code :

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("checkmo"))).click();

Upvotes: 0

Related Questions