Get text of radio button

177 views
Skip to first unread message

ankit yadav

unread,
Jan 24, 2016, 1:15:56 PM1/24/16
to Selenium Users
Hi,

I am using a pretty simple html:

<html>
<body>
<input type="radio" name="color" value="red" checked="checked">Red</input></br>
<input type="radio" name="color" value="green"/>Green</br>
<input type="radio" name="color" value="blue"/>Blue
</body>
</html>

I am trying to get the text of a radio button. I have already tried getText() option but it doesn't work for input type radio button. Can someone please help.

I am using JAVA with chrome driver.

Thanks in advance.

Xiang Dong

unread,
Jan 25, 2016, 1:05:35 AM1/25/16
to seleniu...@googlegroups.com
String js = "return arguments[0].nextSibling.innerHTML";
pageData = (String)jsExecutor.executeScript(js, webElement);

it will retrieve the text  from radio.

Best Regards,
--david




Date: Sun, 24 Jan 2016 10:15:56 -0800
From: ankit.s...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Get text of radio button
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/2beac17a-5216-40e2-a4ee-7f4a899ddc21%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ankit Yadav

unread,
Jan 25, 2016, 1:34:53 AM1/25/16
to seleniu...@googlegroups.com
Thanks David.  
But i want to use JAVA only. Is there a way in JAVA?

--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/wX8MUBqDsDg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.

Xiang Dong

unread,
Jan 25, 2016, 2:37:14 AM1/25/16
to seleniu...@googlegroups.com
I think the getText() method does not works, return innerHTML also does not works.  I have no idea do we have direct WebDriver API to do it.

Best  Regards,
--david


Date: Mon, 25 Jan 2016 12:04:37 +0530
Subject: Re: [selenium-users] Get text of radio button
From: ankit.s...@gmail.com
To: seleniu...@googlegroups.com

Anand Somani

unread,
Jan 25, 2016, 2:41:49 AM1/25/16
to seleniu...@googlegroups.com
Please paste ur code which is not working... it will better to know.

Thanks & Regards,
Anand Somani
Refer myBlogs: http://seleniumtestingguide.blogspot.in/


Xiang Dong

unread,
Jan 25, 2016, 2:54:15 AM1/25/16
to seleniu...@googlegroups.com
BTW, my code paste below is a java code. 

--david


Date: Mon, 25 Jan 2016 13:11:37 +0530

Subject: Re: [selenium-users] Get text of radio button

Anand Somani

unread,
Jan 25, 2016, 3:21:59 AM1/25/16
to seleniu...@googlegroups.com
Try with below xpath 

//input[@value='red']

Thanks & Regards,
Anand Somani
Refer myBlogs: http://seleniumtestingguide.blogspot.in/


Ankit Yadav

unread,
Jan 25, 2016, 3:59:50 AM1/25/16
to seleniu...@googlegroups.com
Below is the code:

List<WebElement> radioBtns = driver.findElements(By.name("color"));
for(WebElement radioBtn:radioBtns){
if(radioBtn.isSelected()){
System.out.println(radioBtn.getText());
}
}


Mark Collin

unread,
Jan 25, 2016, 5:53:04 AM1/25/16
to Selenium Users
You have some invalid markup there.  an <input> element should not have a closing tag.  The text you have between the opening tag and the closing tag is not really related to the <input element> it's just random text. See here for more info:


The text associated with an input element will be placed in the value attribute.  To get at it you would need to do this:

WebElement myRadioButton = driver.findElement(By.cssSelector("input:nth-child(1)"))
String radioButtonText = myRadioButton.getAttribute("value");

ankit yadav

unread,
Jan 27, 2016, 7:52:43 AM1/27/16
to Selenium Users
Hi Mark,

In my code there was no input closing tag, i was trying something so pasted it wrong. Your code gives the attribute value not the radio button text value.

Thanks,
Ankit.

Travis

unread,
Jan 27, 2016, 10:18:42 PM1/27/16
to Selenium Users
If the HTML you posted initially isn't the actual HTML you're working with, it would help immensely for you to re-post with the actual HTML.  If what you posted is in fact the HTML you're working with, then your problem is almost certainly that it's invalid, e.g. the </input> tag and the <input ... /> tag.

Travis
Reply all
Reply to author
Forward
0 new messages