Selenium find_elements_by_xpath 到底怎么用

hellohell · May 22, 2018 · Last by sandy replied at December 03, 2018 · 9768 hits

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个;求解,谢谢各位看官

共收到 2 条回复 时间 点赞
hellohell find_element_by_xpath 怎么用 (2) 中提及了此贴 22 May 14:01

建议你再Chrome里面先试一下: F12后 ctrl+F打开搜索框, 搜索框支持XPATH和css, 可以先看看这2种方法搜索的结果如何.

6个,虽然先定位了//div[@class="well1"],但是再次赋值//input[@type="checkbox"]定位的是//input[@type="checkbox"]的6个元素,不会记忆上一次的值。//div[@class="well1"]//input[@type="checkbox"]是3

hellohell 关闭了讨论 27 Jun 13:41
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up