1、简介javascript
selenium是一个用于Web应用自动化程序测试的工具,测试直接运行在浏览器中,就像真正的用户在操做同样。css
selenium2支持经过驱动真实浏览器(FirfoxDriver,IternetExplorerDriver,OperaDriver,ChromeDriver)html
selenium2支持经过驱动无界面浏览器(HtmlUnit,PhantomJs)java
selenium2和selenium区别在于2中把WebDrive整合在了一块儿python
Selenium 的工做原理:linux
Selenium RC(selenium核心)web
Selenium RC 使用的是javascript注入的方式跟浏览器打交道。这样 Selenium RC 须要启动一个Server,而后将操做页面元素的API 转成javascript脚本,再把这段脚本注入到浏览器中去执行。而经过这种javascript注入的方式一来太依赖翻译成javascript质量的好坏,二来javascript存在同源问题。这使测试变得不那么容易。浏览器
WebDriver(selenium2核心)cookie
与Selenium RC 不一样的是Selenium WebDriver 针对不一样的浏览器进行独立开发Driver,利用浏览器的原生API去直接操做浏览器和页面元素,这样大大提升了测试的稳定性和速度。固然由于不一样的浏览器对Web元素操做和呈现多多少少会存在一些差别,这也就形成如今不一样的浏览器须要有对应不一样的Driver(ChromeDriver ,IEDriver等等)。less
2、安装
第一种方法是:下载源码安装,下载地址(https://pypi.python.org/pypi/selenium)解压并把整个目录放到C:\Python27\Lib\site-packages下面
第二种方法是:能够直接在C:\Python27\Scripts 下输入命令安装 pip install -U selenium
sudo pip install selenium
python能够使用selenium执行javascript,selenium可让浏览器自动加载页面,获取须要的数据。selenium本身不带浏览器,能够使用第三方浏览器如Firefox,Chrome等,也能够使用headless浏览器如PhantomJS在后台执行。
linux下执行的话就不用加executable_path='C:\Python27\Scripts\phantomjs.exe'
from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities dcap = dict(DesiredCapabilities.PHANTOMJS) #设置userAgent dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0 ") obj = webdriver.PhantomJS(executable_path='C:\Python27\Scripts\phantomjs.exe',desired_capabilities=dcap) #加载网址 obj.get('http://wap.95533pc.com')#打开网址 html = obj.page_source 获取源码
obj.save_screenshot("1.png") #截图保存 obj.quit() # 关闭浏览器。当出现异常时记得在任务浏览器中关闭PhantomJS,由于会有多个PhantomJS在运行状态,影响电脑性能
webdriver类中有三个和时间相关的方法:
1.pageLoadTimeout 设置页面彻底加载的超时时间,彻底加载即彻底渲染完成,同步和异步脚本都执行完
2.setScriptTimeout 设置异步脚本的超时时间
3.implicitlyWait 识别对象的智能等待时间
from selenium import webdriver obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe") obj.set_page_load_timeout(5) try: obj.get('http://www.xiaohuar.com') print obj.title except Exception as e: print e
对象的定位是经过属性定位来实现的,这种属性就像人的身份证信息同样,或是其余的一些信息来找到这个对象,那咱们下面就介绍下Webdriver提供的几个经常使用的定位方法
<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
上面这个是百度的输入框,咱们能够发现咱们能够用id来定位这个标签,而后就能够进行后面的操做了
更多具体关于XPath的信息,请具体参照 http://www.cnblogs.com/hyddd/archive/2009/05/22/1487332.html
from selenium import webdriver obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe") obj.set_page_load_timeout(5) try: obj.get('http://www.baidu.com') obj.find_element_by_id('kw') #经过ID定位 obj.find_element_by_class_name('s_ipt') #经过class属性定位 obj.find_element_by_name('wd') #经过标签name属性定位 obj.find_element_by_tag_name('input') #经过标签属性定位 obj.find_element_by_css_selector('#kw') #经过css方式定位 obj.find_element_by_xpath("//input[@id='kw']") #经过xpath方式定位 obj.find_element_by_link_text("贴吧") #经过xpath方式定位 print obj.find_element_by_id('kw').tag_name #获取标签的类型 except Exception as e: print e
from selenium import webdriver obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe") obj.set_page_load_timeout(5) obj.maximize_window() #设置全屏 try: obj.get('http://www.baidu.com') obj.save_screenshot('11.png') # 截取全屏,并保存 except Exception as e: print e
from selenium import webdriver obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe") obj.set_page_load_timeout(5) obj.set_window_size('480','800') #设置浏览器宽480,高800 try: obj.get('http://www.baidu.com') obj.save_screenshot('12.png') # 截取全屏,并保存 except Exception as e: print e
from selenium import webdriver obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe") try: obj.get('http://www.baidu.com') #访问百度首页 obj.save_screenshot('1.png') obj.get('http://www.sina.com.cn') #访问新浪首页 obj.save_screenshot('2.png') obj.back() #回退到百度首页 obj.save_screenshot('3.png') obj.forward() #前进到新浪首页 obj.save_screenshot('4.png') except Exception as e: print e
定位到元素之后,咱们就应该对相应的对象进行某些操做,以达到咱们某些特定的目的,那咱们下面就介绍下Webdriver提供的几个经常使用的操做方法
from selenium import webdriver obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe") obj.set_page_load_timeout(5) try: obj.get('http://www.baidu.com') print obj.find_element_by_id("cp").text # 获取元素的文本信息 obj.find_element_by_id('kw').clear() #用于清除输入框的内容 obj.find_element_by_id('kw').send_keys('Hello') #在输入框内输入Hello obj.find_element_by_id('su').click() #用于点击按钮 obj.find_element_by_id('su').submit() #用于提交表单内容 except Exception as e: print e
一、键盘按键用法
from selenium.webdriver.common.keys import Keys obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe") obj.set_page_load_timeout(5) try: obj.get('http://www.baidu.com') obj.find_element_by_id('kw').send_keys(Keys.TAB) #用于清除输入框的内容,至关于clear() obj.find_element_by_id('kw').send_keys('Hello') #在输入框内输入Hello obj.find_element_by_id('su').send_keys(Keys.ENTER) #经过定位按钮,经过enter(回车)代替click() except Exception as e: print e
from selenium import webdriver from selenium.webdriver.common.keys import Keys obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe") obj.set_page_load_timeout(5) try: obj.get('http://www.baidu.com') obj.find_element_by_id('kw').send_keys(Keys.TAB) #用于清除输入框的内容,至关于clear() obj.find_element_by_id('kw').send_keys('Hello') #在输入框内输入Hello obj.find_element_by_id('kw').send_keys(Keys.CONTROL,'a') #ctrl + a 全选输入框内容 obj.find_element_by_id('kw').send_keys(Keys.CONTROL,'x') #ctrl + x 剪切输入框内容 except Exception as e: print e
selenium2 在python的send_keys()中输入中文会报错,其实在中文前面加一个u变成unicode就能搞定了
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe") try: obj.get("http://pan.baidu.com") obj.find_element_by_id('TANGRAM__PSP_4__userName').send_keys('13201392325') #定位并输入用户名 obj.find_element_by_id('TANGRAM__PSP_4__password').send_keys('18399565576lu') #定位并输入密码 obj.find_element_by_id('TANGRAM__PSP_4__submit').submit() #提交表单内容 f = obj.find_element_by_xpath('/html/body/div/div[2]/div[2]/....') #定位到要点击的标签 ActionChains(obj).context_click(f).perform() #对定位到的元素进行右键点击操做 except Exception as e: print e
二、鼠标双击
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe") try: obj.get("http://pan.baidu.com") obj.find_element_by_id('TANGRAM__PSP_4__userName').send_keys('13201392325') #定位并输入用户名 obj.find_element_by_id('TANGRAM__PSP_4__password').send_keys('18399565576lu') #定位并输入密码 obj.find_element_by_id('TANGRAM__PSP_4__submit').submit() #提交表单内容 f = obj.find_element_by_xpath('/html/body/div/div[2]/div[2]/....') #定位到要点击的标签 ActionChains(obj).double_click(f).perform() #对定位到的元素进行双击操做 except Exception as e: print e
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe") try: obj.get("http://pan.baidu.com") obj.get_cookies() # 获取cookie
obj.get_cookie(name) # 获取指定的cookie except Exception as e: print e
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe") try: obj.get("http://pan.baidu.com") c1 = {u"name":u"ryan"}
obj.add_cookie(c1) # 添加cookie
time.sleep(3)
obj.refresh() #刷新页面
except Exception as e: print e
参考:
http://www.cnblogs.com/yoyoketang/p/6536253.html
http://www.cnblogs.com/luxiaojun/p/6144748.html
http://blog.csdn.net/qq_25794017/article/details/78418091