简体   繁体   中英

How to click on a radio button in selenium webdriver using javascript?

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

Hoping for a resolve.

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();

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?

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