python+selenium—webdriver入门

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 1 # !/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 from selenium import webdriver
 4 import time
 5 
 6 __author__ = "No.1 优测"
 7 
 8 # webdriver 定位元素的八种常用方法
 9 
10  
11 
12 browser = webdriver.Firefox()
13 url = "http://cn.bing.com/"
14 browser.get(url)
15 time.sleep(3)
16 
17 # 1/find element by name
18 browser.find_element_by_name("q").send_keys("python")
19 time.sleep(2)
20 browser.find_element_by_id("sb_form_go").click()
21 time.sleep(2)
22 browser.back()
23 time.sleep(2)
24 # 2/find element by id
25 browser.find_element_by_id("sb_form_q").send_keys("python")
26 time.sleep(2)
27 browser.find_element_by_id("sb_form_go").click()
28 time.sleep(2)
29 browser.back()
30 time.sleep(2)
31 
32 # 3/find element by class name
33 browser.find_element_by_class_name("b_searchbox").send_keys("python")
34 time.sleep(2)
35 browser.find_element_by_id("sb_form_go").click()
36 time.sleep(2)
37 browser.back()
38 time.sleep(2)
39 
40 # 4/find element by tag name
41 browser.find_element_by_tag_name("input").send_keys("python")
42 time.sleep(2)
43 browser.find_element_by_id("sb_form_go").click()
44 time.sleep(2)
45 browser.back()
46 time.sleep(2)
47 
48 # 5/find element by css
49 browser.find_element_by_css_selector("input[title=\\"输入搜索词\\"]").send_keys("python")
50 time.sleep(2)
51 browser.find_element_by_id("sb_form_go").click()
52 time.sleep(2)
53 browser.back()
54 time.sleep(2)
55 
56 # 6/find element by xpath
57 browser.find_element_by_xpath("//input[@id = ‘sb_form_q‘]").send_keys("selennium")
58 time.sleep(2)
59 browser.find_element_by_id("sb_form_go").click()
60 time.sleep(2)
61 browser.back()
62 time.sleep(2)
63 
64 # 7/find element by link text
65 browser.find_element_by_link_text("词典").click()
66 time.sleep(2)
67 browser.find_element_by_id("sb_form_go").click()
68 time.sleep(2)
69 browser.back()
70 time.sleep(5)
71 
72 # 8/find element by partial link text
73 browser.find_element_by_partial_link_text("").click()
74 time.sleep(2)
75 browser.back()
76 time.sleep(2)
77 
78 browser.find_element_by_id("sb_form_go").click()
79 time.sleep(2)
80 browser.find_element_by_id("sb_form_go").click()
81 time.sleep(2)
82 browser.back()
83 time.sleep(2)
84 browser.quit()

2、find elements方法:

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

准备html文件:

技术分享
 1 <html>
 2     <head>
 3         <meta http-equiv="content-type" content="text/html;charset=utf-8" />
 4         <title>Checkbox</title>
 5         <script type="text/javascript" async="" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
 6         <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
 7         <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
 8     </head>
 9     <body>
10         <h3>checkbox</h3>
11         <div class="well">
12             <form class="form-horizontal">
13                 <div class="control-group">
14                     <label class="control-label" for="c1">checkbox1</label>
15                     <div class="controls">
16                         <input type="checkbox" id="c1" />
17                     </div>
18                 </div>
19                 <div class="control-group">
20                     <label class="control-label" for="c2">checkbox2</label>
21                     <div class="controls">
22                         <input type="checkbox" id="c2" />
23                     </div>
24                 </div>
25                 <div class="control-group">
26                     <label class="control-label" for="c3">checkbox3</label>
27                     <div class="controls">
28                         <input type="checkbox" id="c3" />
29                     </div>
30                 </div>    
31         
32                 <div class="control-group">
33                     <label class="control-label" for="r">radio</label>
34                     <div class="controls">
35                         <input type="radio" id="r1" />
36                     </div>
37                 </div>
38                 
39                 <div class="control-group">
40                     <label class="control-label" for="r">radio</label>
41                     <div class="controls">
42                         <input type="radio" id="r2" />
43                     </div>
44                 </div>
45             </form>
46         </div>
47     </body>
48 </html>
View Code

 

 1 #!/usr/bin/env python
 2 #-*- coding:utf-8 -*- 
 4 
 5 import selenium
 6 from selenium import  webdriver
 7 import time
 8 import os
9 # 使用绝对路径用normpath方法,使用相对路径用abspath方法 10 url = "file:///"+os.path.normpath("C:\\Users\\Administrator\\Desktop\\checkbox.html") 11 browser = webdriver.Firefox() 12 browser.get(url) 13 checkboxs = browser.find_elements_by_css_selector("input[type = checkbox]") 14 for checkbox in checkboxs: 15 checkbox.click() 16 time.sleep(2) 17 #取消最后一个按钮选择 18 browser.find_elements_by_css_selector("input[type = checkbox]").pop().click() 19 time.sleep(2) 20 browser.quit()

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

 


以上是关于python+selenium—webdriver入门的主要内容,如果未能解决你的问题,请参考以下文章

Selenium WebDriver(Python)API

[python] python+selenium+webdriver

[python] python+selenium+webdriver

selenium webdriver (python)大全

python+selenium—webdriver入门

转载selenium+Python WebDriver之selenium的定位以及切换frame(iframe)