无法单击跨度Selenium Python中的单选按钮

问题描述 投票:1回答:3

伙计们,

我试图点击跨度内的单选按钮。试图执行:

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)

这是我得到的错误:ElementNotVisibleException:消息:元素不可见

HTML代码:

<!-- 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>

伙计们,谢谢你的关注!

python selenium selenium-webdriver radio-button radio
3个回答
0
投票

根据您共享的HTML,点击<label>旁边的单选按钮,文本为Milhas,您可以使用以下代码行:

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

0
投票

您可以使用此xpath

//div[@class='radio']//span//input[@id='booking-type-1']

//div[@class='radio']//span//input[@id='booking-type-2']

//div[@class='radio is-disabled js-country-eligible']//span//input[@id='booking-type-3']

0
投票

似乎origin元素的下拉列表覆盖了您尝试单击的单选按钮。也许你可以改变操作的顺序?单击destination元素也会打开单选按钮所在的部分,但似乎无法覆盖它。这是单击该单选按钮的快速示例,然后您可以在之后填写origin输入。

from selenium import webdriver

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

destination = driver.find_element_by_id('destination')
destination.click()

miles_box = driver.find_element_by_id('booking-type-2')
miles_box.click()
© www.soinside.com 2019 - 2024. All rights reserved.