1 Selenium Python 基础

1 环境搭建

  • setuptools:setuptools是一组Python的distutilsde工具的增强工具,可以让程序员更方便的创建和发布Python包

       下载地址:https://pypi.python.org/pypi/setuptools#downloads

       安装:cd setuptools目录,执行 python setup.py install

  • pip:pip是python的安装包管理工具

       下载地址:https://pypi.python.org/pypi/pip#downloads

       安装:cd pip目录,执行python setup.py install,安装完毕需要设置环境变量path,C:\Python27\Scripts

  • selenium:UI自动化测试开源工具

       最新版安装:pip install selenium

       指定版安装:pip install selenium==2.48.0

       查看版本:pip show selenium

 2 第一个程序

 那百度的搜索当作第一个例子

selenium3.x开始,webdriver/firefox/webdriver.py的init中,executable_path=”geckodriver”;
而2.x是executable_path=”wires”;
所以需要自己配置geckodriver;
下载地址:https://github.com/mozilla/geckodriver/releases
下载后(根据系统版本选择):

解压取出geckodriver.exe(以64x为例);
将geckodriver.exe放到Firefox的安装目录下,如:(D:\火狐\Mozilla Firefox);
将火狐安装目录(D:\火狐\Mozilla Firefox)添加到环境变量path中
(最终要的一步)重启pycharm,不要像我一样傻逼的装了好几次火狐就不重启一次pycharm

1 from selenium import webdriver
2 
3 driver = webdriver.Firefox()
4 driver.maximize_window()
5 driver.get("https://www.baidu.com/")
6 
7 #搜索
8 driver.find_element_by_xpath("//input[@id='kw']").send_keys("java")
9 driver.find_element_by_xpath("//input[@id='su']").click()
View Code

 3 对象识别

3.1浏览器操作

1 from selenium import webdriver
2 
3 driver = webdriver.Firefox();  #启动浏览器
4 driver.maximize_window();    #浏览器最大化
5 dirver.get("https://www.baidu.com"); #浏览器导航
6 driver.back();          #浏览器后退
7 driver.forward();     #浏览器前进
8 driver.refresh();      #浏览器刷新
View Code

3.2一般操作

 1 from selenium import webdriver
 2 
 3 #链接
 4 linkLogin = driver.find_element_by_xpath("//div[@id='u1']/a[text()='登录']");
 5 linkLogin.click();
 6 
 7 #输入框
 8 textUsername = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_8__userName']");
 9 textUsername.clear();
10 textUsername.sendKeys(“测试开发");
11 
12 #按钮
13 WebElement buttonLogin = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_8__submit']");
14 buttonLogin.click();
15 
16 #悬停
17 my = driver.find_element_by_xpath("//a[@id='s_username_top']/span");
18 action = ActionChains(driver);
19 action.move_to_element(my).perform();
20 
21 #单选
22 #方法1
23 radiosGender = driver.find_element_by_xpath("//input[@id='passport-sex-2']");
24 radiosGender.click();
25 #方法2 – 从0开始
26 radiosGender = driver.find_elements_by_xpath("//input[@name='passport_sex']");
27 radiosGender.pop(0).click();
28 
29 #多选框
30 #方法1
31 xg = driver.find_element_by_xpath("//input[@id='passport-character-1']");
32 xg.click();
33 #方法2 – 从0开始
34 xg = driver.find_elements_by_xpath("//input[@name='passport_character']");
35 xg.pop(0).click();
36 
37 #下拉      
38 from selenium.webdriver.support.ui import Select
39 
40 time =driver.find_element_by_xpath("//div[@id='main-wraper']//select[1]");
41 select = Select(time);
42 select.select_by_visible_text("三个月内");
43 
44 #JS
45 js = "alert('你好');"
46 driver.execute_script(js); 
47 alert =driver.switch_to_alert();
48 alert.accept();
49 
50 #上传文件
51 file = driver.find_element_by_xpath("//div[@id='uploadfile']/input");
52 file.sendKeys("C:\\test.txt");
53 
54 #iframe切换
55 baidu = driver.find_element_by_xpath("//iframe[@id='baidu']");
56 driver.switch_to_frame(baidu);  #切换到iframe
57 ……
58 ……
59 driver.switch_to.parent_frame();  #切换回原窗口
60 
61 #弹出窗口
62 link = driver.find_element_by_xpath("//div[@id='alert']/input");
63 link.click();
64 alert =driver.switch_to_alert();
65 alert.accept();
66 
67 #窗口切换
68 def switchToWindow(title):
69     currentHandle = driver.current_window_handle;
70     handles = driver.window_handles;
71     
72     for handle in handles:
73         if handle == currentHandle:
74             continue;
75         else:
76             driver.switch_to_window(handle);
77             if title in driver.title:
78                 break;
79             else:
80                 continue;
81 switchToWindow("帐号设置"); 
View Code

3.3 鼠标操作

 1 from selenium.webdriver.common.action_chains import ActionChains
 2 
 3 #悬停
 4 element = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']");
 5 action = ActionChains(driver);
 6 action.move_to_element(element).perform();
 7 #右击
 8 element = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']");
 9 action = ActionChains(driver);
10 action.context_click(element).perform();
11 #双击
12 element = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']");
13 action = ActionChains(driver);
14 action.double_click(element).perform();
15 #拖拽
16 source = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']");
17 target = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']");
18 action = ActionChains(driver);
19 action.drag_and_drop(source, target).perform();
View Code

3.4 键盘操作

 1 from selenium.webdriver.common.keys import Keys
 2 
 3 #全选
 4 element = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']");
 5 element.send_keys(Keys.CONTROL,"a");
 6 #复制
 7 element = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']");
 8 element.send_keys(Keys.CONTROL,“c");
 9 #粘贴
10 element = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']");
11 element.send_keys(Keys.CONTROL,“v");
12 #回车
13 element = driver.find_element_by_xpath("//div[@id='u1']/a[text()=‘XX']");
14 element.send_keys(Keys.ENTER);
View Code
posted on 2017-03-26 10:35  栗子测试  阅读(217)  评论(0编辑  收藏  举报