Reputation: 5008
I am using webdriver for automation and each question has some radio buttons with some text like yes/no beside the radio button. following is the code I get by inspecting element
<label class="fs-508-label" for="editTemplate-editForm-content-flowTemplateFull-genericPage-_id162-page_0-profilerQuestionnaireBlock-_id167-page__1-questionnaire-_id169-0-questionRadio_com.taleo.functionalcomponent.prescreening.entity.question.PossibleAnswer__78620">
<input name="editTemplate-editForm-content-flowTemplateFull-genericPage-_id162-page_0-profilerQuestionnaireBlock-_id167-page__1-questionnaire-_id169-0-questionRadio" id="editTemplate-editForm-content-flowTemplateFull-genericPage-_id162-page_0-profilerQuestionnaireBlock-_id167-page__1-questionnaire-_id169-0-questionRadio_com.taleo.functionalcomponent.prescreening.entity.question.PossibleAnswer__78620" value="com.taleo.functionalcomponent.prescreening.entity.question.PossibleAnswer__78620" type="radio" class="possibleanswers" onclick="onChoicePicked(this)">
No
</label>
can you please help me figure it out. I am currently able to get the radio button using
element = browser.find_element_by_name(name got form above)
but element.text is not giving me 'No' but empty which is baffling me. I might be missing some thing here.
Upvotes: 1
Views: 2489
Reputation: 2031
Your element is empty and this is correct behaviour. The thing is, that by doing browser.find("idblahblah") you simply get only the radio button element itself, not the text as this is other element. The text itself is an element that can be found by doing driver.findElement(By.className("fs-508-label")) however this is not the best solution as class values tend to change. I would recommend changing the html for this a little bit to sth like this:
<input id="true_radio_button" type="radio" name="bar"
value="bar_radio_true" checked="checked"/>
<label id="whatever" for="true_radio_button">True</label>
This way you can now easily find the radio button using ID and then find corresponding Label. Checking if it's the correct one can be done by checking if the 'for' attribute is equal to the radio button ID - label.getAttribute("for") equal to radio_button_ID
Edit: Using this: driver.findElement(By.className("fs-508-label")) should work just fine
Upvotes: 1