【问题标题】:How do to check and uncheck other checkbox in a loop one at same time?如何同时选中和取消选中一个循环中的其他复选框?
【发布时间】:2020-09-29 06:14:53
【问题描述】:

我有三个带有 id 的复选框

id = ["inputListings_I","inputListings_A","inputListings_B"]

我的问题:使用循环,我想一次选中一个复选框并取消选择另一个。假设我选择 id[0] 然后循环选择 id[1] 但它应该取消选中 id[0],如果循环选择 id[2] 它应该取消选中 id[1]。

怎么做?

lst = ["inputListings_I","inputListings_A","inputListings_B"]

for i in lst:
    mm = driver.find_element_by_id(i)
    if (mm.is_selected()):
        js = f"document.getElementById({i}).click()"
        driver.execute_script(js)
    js = f"document.getElementById('{i}').click()"
    driver.execute_script(js)"
    """unselect other two"""

【问题讨论】:

    标签: javascript python for-loop selenium-chromedriver


    【解决方案1】:

    不要随意切换它们,只需设置 checked 属性:

    lst = ["inputListings_I","inputListings_A","inputListings_B"]
    
    choice = "inputListings_A"
    for i in lst:
        js = f"document.getElementById({i}.checked = {i == choice}"
        driver.execute_script(js)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2013-07-21
      • 2020-08-12
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 2013-10-11
      相关资源
      最近更新 更多