Search code examples
pythonseleniumselenium-webdriverradio-button

Cannot click a radio button inside a span Selenium Python


I am trying to click into a radio button inside a span. Trying to execute:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys 


chromeDriver = webdriver.Chrome()
chromeDriver.get('https://www.flytap.com/pt-br/')

#Works Fine
chromeDriver.find_element_by_id('origin')
origin.click()
origin.clear()
origin.send_keys('(RIO) Rio De Janeiro -  todos os aeroportos , Brasil')
    origin.click()

#Where I got the error
milesBox = chromeDriver.find_element_by_id("booking-type-2").click()

#Also tried:
milesBox = chromeDriver.find_element_by_id("booking-type-2").send_keys(Keys.ENTER)

#And, finally:
milesBox = chromeDriver.find_element_by_id("booking-type-2").send_keys(Keys.SPACE)

Here is the error I got:

ElementNotVisibleException: Message: element not visible

HTML code:

<!-- Payment Methods START -->
                <fieldset id="booking-payment-drag" class="booking-payment">
                    <div class="toolbar-radio-wrapper">
                        <legend class="ipt-label">Reservar com:</legend>
                    </div>
                    <div class="toolbar-radio-wrapper">
                        <div class="radio">
                            <span class="checked"><input type="radio" id="booking-type-1" name="booking-type" value="1" checked></span>
                        </div>
                        <label for="booking-type-1" class="ipt-label">Dinheiro</label>
                    </div>
                    <div class="toolbar-radio-wrapper js-country-not-eligible">
                        <div class="radio">
                            <span class=""><input type="radio" id="booking-type-2" name="booking-type" value="2"></span>
                        </div>
                        <label for="booking-type-2" class="ipt-label">Milhas</label>
                    </div>
                        <div class="toolbar-radio-wrapper">
                            <div class="radio is-disabled js-country-eligible">
                                <span class=""><input type="radio" id="booking-type-3" name="booking-type" value="3" disabled></span>
                            </div>
                            <label for="booking-type-3" class="ipt-label">Miles&Cash</label>
                                                        </div>
                </fieldset>

Solution

  • As per the HTML you have shared to click on the radio button beside the <label> with text as Milhas you can use the following line of code :

    chromeDriver.find_element_by_xpath("//input[@id='booking-type-2' and @name='booking-type']").click()