【问题标题】:How to make selenium click on a radio button如何使硒单击单选按钮
【发布时间】:2021-08-12 08:50:54
【问题描述】:

我想在 Python 3.9 中使用 Selenium 来点击按钮。

https://www.sepaq.com/en/reservation/national-parks/annual-card

我想点击生效日期,然后点击 2021 年 7 月,然后点击确定。

前两个工作正常,然后我添加了代码以单击确定按钮,我无法再单击 2021 年 7 月。有趣的是单击确定步骤也有效。

错误我收到此错误:

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=90.0.4430.212)

我试过了:

  1. sleep 语句使其等待按钮变为可用
  2. Web 驱动程序等待
  3. 动作链使其滚动到视图中

我正在使用带有 chrome 的 Selenium IDE 来为我提供 css 标签。

from time import sleep

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options 
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

def print_hi():


    chrome_options = Options()
    chrome_options.add_experimental_option("detach", True)
    chrome_options.add_argument("start-maximized")
    chrome_options.add_argument("disable-infobars")
    chrome_options.add_argument("--disable-extensions")


    annual_pass = webdriver.Chrome(options=chrome_options)
    annual_pass.get("https://www.sepaq.com/en/reservation/national-parks/annual-card")

    type = annual_pass.find_element_by_css_selector('.form-list:nth-child(2) > li:nth-child(1) .form-label')
    type.click()

    annual_pass.get("https://www.sepaq.com/en/reservation/national-parks/annual-card")
    open_eff_date_panel = annual_pass.find_element_by_link_text('Select')
    open_eff_date_panel.click()

    annual_pass.get("https://www.sepaq.com/en/reservation/national-parks/annual-card")
    selectJuly = annual_pass.find_element_by_css_selector('li:nth-child(3) .form-label')
    #selectJuly = annual_pass.find_element_by_xpath('//*[@id="date2"]')
    selectJuly.click() ## error here##

    annual_pass.get("https://www.sepaq.com/en/reservation/national-parks/annual-card")
    ok_button = annual_pass.find_element_by_css_selector('#reserver-date .bouton')
    ok_button.click()

    sleep(10)
    annual_pass.close() 
if __name__ == '__main__':
    print_hi() 

【问题讨论】:

    标签: python css selenium selenium-webdriver xpath


    【解决方案1】:

    使用 Selenium 时,单击切换可能会不稳定。

    首先,试试这个方法:

    selectJuly = annual_pass.find_element_by_css_selector('#reserver .panel.is-active>.panel-middle li:nth-child(3) .form-label')
    annual_pass.execute_script("arguments[0].click();", selectJuly)
    ok_button = annual_pass.find_element_by_css_selector('#reserver-date .bouton')
    ok_button.click()
    

    它可以工作,但我不喜欢窗口没有完全展开。

    带有调试信息的完整代码:

    import time
    from time import sleep
    
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.remote.webelement import WebElement
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    def print_hi():
    
    
        chrome_options = Options()
        chrome_options.add_experimental_option("detach", True)
        chrome_options.add_argument("start-maximized")
        chrome_options.add_argument("disable-infobars")
        chrome_options.add_argument("--disable-extensions")
    
    
        annual_pass = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver', options=chrome_options)
        annual_pass.get("https://www.sepaq.com/en/reservation/national-parks/annual-card")
    
        open_eff_date_panel = annual_pass.find_element_by_link_text('Select')
        open_eff_date_panel.click()
        # wait = WebDriverWait(open_eff_date_panel, 10)
        # wait.until(EC.visibility_of_element_located(
        #     (By.CSS_SELECTOR, "//html[@class='js history es5array video opacity csspointerevents placeholder inlinesvg es5date supports es5function es5object strictmode es5string json es5syntax es5undefined es5 no-touchevents cssvhunit mediaqueries vibrate csscolumns csscolumns-width csscolumns-span csscolumns-fill csscolumns-gap csscolumns-rule csscolumns-rulecolor csscolumns-rulestyle csscolumns-rulewidth csscolumns-breakbefore csscolumns-breakafter csscolumns-breakinside flexbox csstransforms csstransforms3d no-ios panel-active']")))
        selectJuly = annual_pass.find_element_by_css_selector(
            '#reserver .panel.is-active>.panel-middle li:nth-child(3) .form-label')
        annual_pass.execute_script("arguments[0].click();", selectJuly)
        ok_button = annual_pass.find_element_by_css_selector('#reserver-date .bouton')
        ok_button.click()
    
        # annual_pass.close()
    if __name__ == '__main__':
        print_hi() 
    

    2 另外,您可以尝试实现自己的函数,以等待表单的某些 css 属性发生更改(例如宽度)。为此,请使用 Selenium 的 value_of_css_property

    3 如果您等待所有类在 xpath 中可用,它也可以工作,但看起来并不健壮。您可以将此定位器更改为css并逐个排除类以使其更短。只等待panel-active 类是行不通的:

    wait = WebDriverWait(open_eff_date_panel, 10)
    wait.until(EC.visibility_of_element_located(
        (By.XPATH, "//html[@class='js history es5array video opacity csspointerevents placeholder inlinesvg es5date supports es5function es5object strictmode es5string json es5syntax es5undefined es5 no-touchevents cssvhunit mediaqueries vibrate csscolumns csscolumns-width csscolumns-span csscolumns-fill csscolumns-gap csscolumns-rule csscolumns-rulecolor csscolumns-rulestyle csscolumns-rulewidth csscolumns-breakbefore csscolumns-breakafter csscolumns-breakinside flexbox csstransforms csstransforms3d no-ios panel-active']")))
    selectJuly = annual_pass.find_element_by_css_selector('#reserver .panel.is-active>.panel-middle li:nth-child(3) .form-label')
    selectJuly.click()
    

    我也试过了,wait.until(lambda driver: driver.execute_script("return jQuery.active == 0")),但它不适用于这种情况。

    我也有类似的问题:Cannot click on toggle with selenium because of ElementNotInteractableException

    【讨论】:

    • 奇怪的是,execute_script 和 css 不起作用,起作用的是在 selectJuly 之前删除了 Annual_pass.get:#annual_pass.get("sepaq.com/en/reservation/national-parks/annual-card")
    • 有趣的是,当我执行代码时,它并不总是在删除这个 Annual_pass.get() 的情况下工作。我根本没有考虑它,因为我认为您只是将代码粘贴错了。所以我的建议是在您打开网站一次并使用此链接时删除其他 annual_pass.get("https://www.sepaq.com/en/reservation/national-parks/annual-card")
    • 当我研究这个时,我遇到了一篇关于当你点击页面时 DOM 发生了变化的帖子,所以新的 get 会刷新 DOM。但是这种方法似乎真的很不稳定,因为现在点击 7 月又开始失败了
    • 大声笑它正在工作,现在它停止了,我不知道为什么......
    • 检查更新的答案并接受/赞成它有效)
    猜你喜欢
    • 2018-11-02
    • 1970-01-01
    • 2021-12-08
    • 2021-09-19
    • 2021-03-17
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多