el323
el323

Reputation: 2920

how to select a radio button in selenium python?

<ul>

                    <li>
                        <label class="checkbox">
                            <input checked="checked" id="fechapublicacion" name="fechapublicacion" onclick="javascript: filtrarListado(&#39;fechapublicacion&#39;, this);" type="radio" value="0" />
                            <span>Cualquier fecha</span>
                        </label>
                        <input id="fechapublicacionhidden" name="fechapublicacionhidden" type="hidden" value="0" />
                    </li>
                    <li>
                        <label class="checkbox">
                            <input id="fechapublicacion" name="fechapublicacion" onclick="javascript: filtrarListado(&#39;fechapublicacion&#39;, this);" type="radio" value="1" />
                            <span>&#218;ltimas 24 horas</span>
                        </label>
                        <input id="fechapublicacionhidden" name="fechapublicacionhidden" type="hidden" value="0" />
                    </li>
                    <li>
                        <label class="checkbox">
                            <input id="fechapublicacion" name="fechapublicacion" onclick="javascript: filtrarListado(&#39;fechapublicacion&#39;, this);" type="radio" value="7" />
                            <span>&#218;ltimos 7 d&#237;as</span>
                        </label>
                        <input id="fechapublicacionhidden" name="fechapublicacionhidden" type="hidden" value="0" />
                    </li>
                    <li>
                        <label class="checkbox">
                            <input id="fechapublicacion" name="fechapublicacion" onclick="javascript: filtrarListado(&#39;fechapublicacion&#39;, this);" type="radio" value="15" />
                            <span>&#218;ltimos 15 d&#237;as</span>
                        </label>
                        <input id="fechapublicacionhidden" name="fechapublicacionhidden" type="hidden" value="0" />
                    </li>
            </ul>

I am trying to select the last radio button. Since all of them have same id, how can i select the last one.

Any help will be appreciated.

Upvotes: 0

Views: 4540

Answers (2)

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

Actually find_elements returns list that's why you are in trouble, you should try using find_element instead as below :-

browser.find_element_by_id("fechapublicacion").click()

Or if you want to use find_elements, You should try using index in returned list as below :-

browser.find_elements_by_id("fechapublicacion")[0].click()

Edited :- As I'm seeing in provided HTML, I'd is not unique there and you want to select last element with the id then try using find_elements and select last element by passing last index as -1 :

browser.find_elements_by_id("fechapublicacion")[-1].click()

Or you can also use find_element to point this element using unique css_selector as below :-

browser.find_element_by_css_selector("input#fechapublicacion[value='15']").click()

Upvotes: 2

Grasshopper
Grasshopper

Reputation: 9058

You are using find_elements.... which returns a list. Use findelement or getelement which returns one element(i got no idea abt python functions). Or else use an index 0 to findelements. it is saying it is trying yo find a click method on a list which it aint got

Upvotes: 0

Related Questions