• 'WebElement' object is not iterable


    checkbox.html源码:

        <html>
            <head>
                <meta http-equiv="content-type" content="text/html;charset=utf-8" />
                <title>Checkbox</title>
                <script type="text/javascript" async="" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
                <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
                <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
            </head>
            <body>
                <h3>checkbox</h3>
                <div class="well">
                    <form class="form-horizontal">
                        <div class="control-group">
                            <label class="control-label" for="c1">checkbox1</label>
                            <div class="controls">
                                <input type="checkbox" id="c1" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="c2">checkbox2</label>
                            <div class="controls">
                                <input type="checkbox" id="c2" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="c3">checkbox3</label>
                            <div class="controls">
                                <input type="checkbox" id="c3" />
                            </div>
                        </div>                      
                        <div class="control-group">
                            <label class="control-label" for="r">radio</label>
                            <div class="controls">
                                <input type="radio" id="r" />
                            </div>
                        </div>                      
                    </form>
                </div>
            </body>
        </html>
    

      Python代码:

    #coding=utf-8
    
    from selenium import webdriver
    import time
    import os
    
    dr = webdriver.Firefox()
    file_path =  'file:///' + os.path.abspath('checkbox.html')
    dr.get(file_path)
    
    # 选择所有的checkbox并全部勾上
    
    checkboxes = dr.find_element_by_css_selector('input[type=checkbox]')
    for check in checkboxes:
        check.click()
    time.sleep(1)
    
    # 打印当前页面上有多少个checkbox
    
    
    dr.quit()
    

      报错内容:TypeError: 'WebElement' object is not iterable

    解决办法:

    checkboxes = dr.find_element_by_css_selector('input[type=checkbox]')
    

      改为:

    checkboxes = dr.find_elements_by_css_selector('input[type=checkbox]')
    

      



  • 相关阅读:
    Linux中的bin文件夹
    ubuntu安装deb包(dpkg)
    vim显示行号
    vim搜索结果高亮
    less和vim中使用正则表达式搜索
    正则表达式中的Quantifiers
    正则表达式解英语单词字谜
    [HDOJ4911]Inversion
    [LightOJ1282]Leading and Trailing
    [HDOJ1568]Fibonacci
  • 原文地址:https://www.cnblogs.com/xiaobaichuangtianxia/p/3732442.html
Copyright © 2020-2023  润新知