0

I am trying to run a test script that will click a radio button. The radio button elements are inside a div.

Here is the javascript code:

var driver = new Builder().forBrowser("chrome").build();
await driver.get("URL");
var selectGender = driver.findElement(By.css("div[class='gender-select-child'] label[for='male-child0']"));
await selectGender.click();

HTML snapshot:

HTML code

Hoping for a resolve.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Brian
  • 1
  • 1
  • Much better if you use the label id for selecting the element : e.g By.css("#gender-radio-parent") In case you want to select the radio button better apply click on the input not label in this case so By.css("#male-child0") – Емил Цоков Aug 02 '22 at 08:59

2 Answers2

0

To click on the with text as Male you can use either of the following locator strategies:

var driver = new Builder().forBrowser("chrome").build();
await driver.get("URL");
var selectGender = driver.findElement(By.css("div[class='gender-select-child'] input[id^='male']"));
await selectGender.click();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

I was able to click the radio button using this syntax...

await driver.findElement(By.css("label[for='female-child0']")).click();

However, I need to use it twice for it to work. My script looks like this...

Javascript snap

Any idea why?

aaandri98
  • 585
  • 4
  • 18
Brian
  • 1
  • 1