python - 使用 Selenium 检查所有复选框

标签 python python-3.x selenium checkbox

我正在尝试检查页面上的所有复选框,类似于 https://sqa.stackexchange.com/questions/3292/how-to-select-or-check-multiple-checkboxes-in-selenium

我尝试用

来做到这一点
checkboxes = driver.find_elements_by_class_name('gwt-CheckBox')
for checkbox in checkboxes:
    if not checkbox.is_selected():
        checkbox.click()

但是,所有复选框的 is_selected() 都给出 False,因此已选择的复选框将关闭,未选择的复选框将打开。下面是一个选中的 CheckBox 类(库名称)和一个未选中的 Checkbox 类(配对标称长度)的示例。

如何检查这些是否已被选择?

  <div style="position: relative; display: inline-block; vertical-align: top; float: left;">
   <span class="gwt-CheckBox">
    <input id="gwt-uid-27" tabindex="0" type="checkbox" value="on"/>
    <label for="gwt-uid-27">
     Library name
    </label>
   </span>
  </div>
 </div>
 <div class="holderFp" style="width: 100%; position: relative; overflow: hidden; display: block;">
  <div style="position: relative; display: inline-block; vertical-align: top; float: left; width: 25%;">
   <span class="gwt-CheckBox">
    <input id="gwt-uid-28" tabindex="0" type="checkbox" value="on"/>
    <label for="gwt-uid-28">
     Paired nominal length
    </label>
   </span>
  </div>

最佳答案

看来复选框元素实际上在 <input> 中子标签。尝试使用它

checkboxes = driver.find_elements_by_css_selector('.gwt-CheckBox > input')

关于python - 使用 Selenium 检查所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41345961/

相关文章:

python - 在python中最快计算512位数的最大质因数

python - "new text"标签随机出现在 Plotly 图中

python - 文件 csv 中每行中间的空格

python - Node.js 使用 chrome-remote-interface 进行抓取

java - 将 Selenium 浏览器脚本转换为 Jmeter 脚本的简单方法?

python - Django 查询在调用后是否保存其结果?

Python 程序,计算掷公平骰子 12 次时至少两次获得 1 的概率

python - 在 for 循环中分离打印

python - 无论如何,是否可以在没有显示器的情况下运行机器人框架测试?

python - 如何根据现有值与上下边界的比较来创建新的 Pandas 系列