How To Select Radio Button

How To Select Radio Button on Synthetics Scripts

Hi, @sushil173: It will depend how the radio button is defined on the page; Synthetics can use CSS selectors to find an element. If the element has a unique ID, findElement(By.Id) is the most efficient way to select it. Otherwise, you may use a different CSS selector to locate it.

It would helpful for us to see how the radio button(s) are defined on the page; can you post a short snippet of HTML? In the meantime, you may find this video helpful in learning how to use Synthetics to select page elements: https://learn.newrelic.com/course/intro_synthetics/writing_scripts.

1 Like

i want to select radio button at mbasic.facebook.com (Gender section).

OK, here is how to determine the selector for an HTML element:

  1. Visit the target page in a browser with developer tools. In this example, I am using Google Chrome and visiting https://mbasic.facebook.com/reg/.

  2. Right-click on the desired element and select Inspect element.

  3. A developer tools pane should open in the browser, showing the element’s HTML code.

  4. If the element has an id attribute, you’re done: you may select the element by its ID.

  5. If the element does not have an id, note the trail of breadcrumbs leading to the element. In this example, you want to select an input that is a descendant of an element with an ID of #sex:

  1. Type Cmd + F or Ctrl + F to open a search bar, and experiment with CSS or XPath selectors until you find one that selects the desired element.

In this example, you may use #sex input[value="1"] to select the Female option, or #sex input[value="2"] to select Male.

Understanding CSS selectors is very helpful when writing browser scripts.

1 Like

@philweber Thank you too much.

@philweber how can i select month(dropdown) at https://accounts.google.com/SignUp (Birthday section) ???

Hi, @sushil173: See if you can use the technique in my previous post to figure it out. :slight_smile:

Rather than trying to use Synthetics to automate web forms, you may find this technique easier to use:

1 Like

Thanks, @philweber have you inspect & look at https://accounts.google.com/SignUp (Birthday/months section) ???
During signup at Gmail, your above technique is fully applicable?

I did look at it, that’s why I posted my second reply.

Hi, @philweber , i am unable to select months on gmail signup. help me

I recommend that, rather than trying to automate the signup form, you use the technique described below to post the desired values directly to the form’s target:

That is all the help I can provide. Perhaps another member of the community will be able to provide additional help.

1 Like

hello @philweber i think you are the only one genius member in community so you can help me by providing code.

1 Like

I want to sign up at mbasic.facebook.com via scripts, help me… i am stuck on Radio Button.

var assert = require(‘assert’);
$browser.get(‘https://mbasic.facebook.com/’)
.then(function(){
$browser.findElement($driver.By.className(“m n o p q s”)).click();
})
.then(function(){
$browser.findElement($driver.By.name(“firstname”)).sendKeys(“Abc”) ;
}).then(function(){
$browser.findElement($driver.By.name(“lastname”)).sendKeys(“Abc”) ;
}).then(function(){
$browser.findElement($driver.By.name(“reg_email__”)).sendKeys("abcxyz@gmail.com") ;
}).then(function(){
//Here choose Radio Button.(Help ME here)
})
;

Hey @sushil173 - It looks like my colleague @philweber is helping you out over on your other post for these questions:

Let us know if you have any more questions.

Hi, @RyanVeitch , @philweber help me a lot. Thanks to him.
But now i am stuck here, Help me out.

var assert = require(‘assert’);
var qs = require(‘querystring’);
$browser.get(‘https://accounts.google.com/signUp’)
.then(function(){
$browser.findElement($driver.By.id(“FirstName”)).sendKeys(“Ram”);
})
.then(function(){
$browser.findElement($driver.By.id(“LastName”)).sendKeys(“lama”) ;
}).then(function(){
$browser.findElement($driver.By.id(“GmailAddress”)).sendKeys("abc173xyz@gmail.com") ;
}).then(function(){
$browser.findElement($driver.By.id(“Passwd”)).sendKeys(“REDACTED_PASSWORD”) ;
}).then(function(){
$browser.findElement($driver.By.id(“PasswdAgain”)).sendKeys(“REDACTED_PASSWORD”) ;
}).then(function(){
// StuckHere
})
;

hi, @philweber help me on this topic

@sushil173: Here is a code snippet that I use to select a radio button (hope it helps):

.then(function() {
log(8, ‘Find and Click the “LongDate” radio button to select it’);
return $browser.waitForAndFindElement(By.xpath(’//input[@type=“radio” and @id=“LongDate”]’), DefaultTimeout);
})

.then(function(el) {
el.isSelected()

.then(function(bool) {
if (!bool) {
el.click();
}
});
})

Sorry about the indentation. :frowning: