我有三个带有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"""
与其随意切换,不如设置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)