python+selenium—webdriver入门(二)

本文中主要介绍webdriver常见的对象定位方法

一、对象定位的目的

二、常见的对象定位方法


一、对象定位的目的:

1、操作对象

2、获得对象的属性,如:对象的class属性、name属性等

3、获得对象的text

4、获取对象的数量

二、常见的对象定位方法:

1、find element方法:

  1、id

  2、name

  3、class name

  4、tag name

  5、css定位

  6、xpath定位

  7、link text

  8、partial link text

 # !/usr/bin/env python
# -*- coding:utf-8 -*-
from selenium import webdriver
import time __author__ = "No.1 优测" # webdriver 定位元素的八种常用方法 browser = webdriver.Firefox()
url = "http://cn.bing.com/"
browser.get(url)
time.sleep(3) # 1/find element by name
browser.find_element_by_name("q").send_keys("python")
time.sleep(2)
browser.find_element_by_id("sb_form_go").click()
time.sleep(2)
browser.back()
time.sleep(2)
# 2/find element by id
browser.find_element_by_id("sb_form_q").send_keys("python")
time.sleep(2)
browser.find_element_by_id("sb_form_go").click()
time.sleep(2)
browser.back()
time.sleep(2) # 3/find element by class name
browser.find_element_by_class_name("b_searchbox").send_keys("python")
time.sleep(2)
browser.find_element_by_id("sb_form_go").click()
time.sleep(2)
browser.back()
time.sleep(2) # 4/find element by tag name
browser.find_element_by_tag_name("input").send_keys("python")
time.sleep(2)
browser.find_element_by_id("sb_form_go").click()
time.sleep(2)
browser.back()
time.sleep(2) # 5/find element by css
browser.find_element_by_css_selector("input[title=\"输入搜索词\"]").send_keys("python")
time.sleep(2)
browser.find_element_by_id("sb_form_go").click()
time.sleep(2)
browser.back()
time.sleep(2) # 6/find element by xpath
browser.find_element_by_xpath("//input[@id = 'sb_form_q']").send_keys("selennium")
time.sleep(2)
browser.find_element_by_id("sb_form_go").click()
time.sleep(2)
browser.back()
time.sleep(2) # 7/find element by link text
browser.find_element_by_link_text("词典").click()
time.sleep(2)
browser.find_element_by_id("sb_form_go").click()
time.sleep(2)
browser.back()
time.sleep(5) # 8/find element by partial link text
browser.find_element_by_partial_link_text("地").click()
time.sleep(2)
browser.back()
time.sleep(2) browser.find_element_by_id("sb_form_go").click()
time.sleep(2)
browser.find_element_by_id("sb_form_go").click()
time.sleep(2)
browser.back()
time.sleep(2)
browser.quit()

2、find elements方法:

适用情况:1、批量处理元素;2、批量获取页面元素,过滤部分元素进行定位;

准备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="r1" />
</div>
</div> <div class="control-group">
<label class="control-label" for="r">radio</label>
<div class="controls">
<input type="radio" id="r2" />
</div>
</div>
</form>
</div>
</body>
</html>
 #!/usr/bin/env python
#-*- coding:utf-8 -*- import selenium
from selenium import webdriver
import time
import os
# 使用绝对路径用normpath方法,使用相对路径用abspath方法
url = "file:///"+os.path.normpath("C:\Users\Administrator\Desktop\checkbox.html")
browser = webdriver.Firefox()
browser.get(url)
checkboxs = browser.find_elements_by_css_selector("input[type = checkbox]")
for checkbox in checkboxs:
checkbox.click()
time.sleep(2)
#取消最后一个按钮选择
browser.find_elements_by_css_selector("input[type = checkbox]").pop().click()
time.sleep(2)
browser.quit()

此文参考http://www.cnblogs.com/fnng/p/3190966.html

上一篇:包管理器Bower使用手册之一


下一篇:# advanced packaging