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="c34">checkbox3</label>
                    <div class="controls">
                        <input type="checkbox" id="c34" />
                    </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>


        <div class="well1" name='well1'>
            <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="c33">checkbox3</label>
                    <div class="controls">
                        <input type="checkbox" id="c33" />
                    </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>

测试代码

# coding=utf-8


url='file:///Users/yeap/local/personal/master/selenium_/webdriver_guide/09/checkbox.html'

from selenium import webdriver

ff=webdriver.Firefox()
ff.get(url)


e=ff.find_element_by_xpath('//div[@class="well1"]')
print('*'*10)
print e.get_attribute('name') # 输出 well1
print('*'*10)

e=e.find_elements_by_xpath('//input[@type="checkbox"]')
print len(e) # 输出6 不是3
for _ in e:
    print('*'*10)
    print _.get_attribute('id')
    print('*'*10)

ff.quit()

搞不懂清楚的是为何 e=ff.find_element_by_xpath('//div[@class="well1"]') 定位后,
使用定位后的 e 再次定位//input[@type="checkbox],
结果打印出来的是 6 个;按照我的理解,应该是 3 个;求解,谢谢各位看官


↙↙↙阅读原文可查看相关链接,并与作者交流