How to get the selected radio button label text?

3,381 views
Skip to first unread message

jyo rani

unread,
Mar 8, 2012, 11:29:18 PM3/8/12
to webdriver
I was able to select a radio button based

Html code of a question that contains two answers.Question is -

What kind of location is this?

Home/Residence
Business


Both the answers are radio buttons.What i am trying to achieve is -
if i select "Home/Residence" radio button - i need to be able to get
that text as String(Home/Residence).

<div id="question-container-7735" class="question-container">
<div class="questionTitle "><span
class="questioncolor"><b>What kind of location is this?</b> <span
style="color: #f00;">*</span></span></div>
<div class="questionRow">
<div class="questionColumn ">
<div class="questionRadio">
<div class="inputContainer"><input type='radio' name='7735'
id='id_7735_20767' value='20767' checked="checked" /></div><div
class="labelContainer"><label for='id_7735_20767'><span
class="questioncolor">Home/Residence</span></label></div>

</div>
<div class="questionClear"></div></div>
</div>
<div class="questionRow">
<div class="questionColumn ">
<div class="questionRadio">
<div class="inputContainer"><input type='radio' name='7735'
id='id_7735_20766' value='20766' /></div><div
class="labelContainer"><label for='id_7735_20766'><span
class="questioncolor">Business</span></label></div>

</div>

Sasi kumar

unread,
Mar 9, 2012, 1:41:51 AM3/9/12
to webdriver
Did u tried getAttribute?

Abhijeet Bedagkar

unread,
Mar 9, 2012, 1:52:33 AM3/9/12
to webd...@googlegroups.com

please try following code,worked for me

string test = (string)_remoteWebDriver.ExecuteScript("return document.getElementById('htmlidoftheradiobutton').value");

Did u tried getAttribute?
--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.


Vikas Ojha

unread,
Mar 9, 2012, 2:31:18 AM3/9/12
to webd...@googlegroups.com
Just tell me that how are you selecting a particular radio button.
______________________________________________________________________________________________________
NOTICE: This email and any files transmitted with it, including their content, are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email by mistake, or in error, please notify the sender and/or system manager immediately and delete this e-mail from your system. If you are not the named or intended addressee of this email then you should not disseminate, distribute, copy or disclose this e-mail and you are strictly prohibited from taking any action in reliance on the information contained in this email and any files attached with it.


Although the company has taken reasonable precautions to ensure no viruses are present in this email, the company cannot accept responsibility for any loss or damage arising from the use of this email or attachments.
______________________________________________________________________________________________________

darrell

unread,
Mar 9, 2012, 8:46:58 AM3/9/12
to webdriver
How did you select the radio button? I usually start with the text and
find the radio button...

WebElement label = driver.findElement(By.xpath("//
label[text()='Home/Residence']"));
String for = label.getAttribute("for");
WebElement button = driver.findElement(By.id(for));

If you have the WebElement for the radio button, it would not be too
different...

WebElement radio; // initial this some how
String id = radio.getAttribute("id");
WebElement label = driver.findElement(By.cssSelector("label[for='"
+ id + "'"));
String text = label.getText();

This is assuming the code follows the convention of the FOR attribute
of the LABEL matches the ID attribute of the INPUT.

Darrell

jyo rani

unread,
Mar 9, 2012, 9:24:57 PM3/9/12
to webd...@googlegroups.com
I used for attribute.Works perfect.Thanks!

Reply all
Reply to author
Forward
0 new messages