# Example
from splinter import Browser
with Browser() as browser:
# Visit URL
url = "http://www.google.com"
browser.visit(url)
browser.fill('q', 'splinter - python acceptance testing for web applications')
# Find and click the 'search' button
button = browser.find_by_name('btnG')
# Interact with elements
button.click()
if browser.is_text_present('splinter.readthedocs.io'):
print("Yes, the official website was found!")
else:
print("No, it wasn't found... We need to improve our SEO techniques")
# browser type
browser = Browser('chrome')
browser = Browser('firefox')
browser = Browser('zope.testbrowser')
# Managing Windows
browser.windows # all open windows
browser.windows[0] # the first window
browser.windows["window_name"] # the window_name window
browser.windows.current # the current window
browser.windows.current = browser.windows[3] # set current window to window 3
# splinter api不提供但是可以通过其他来搞定的,比如通过driver来设置window的大小。
browser.driver.set_window_size(1600, 1000)
window = browser.windows[0]
window.is_current # boolean - whether window is current active window
window.is_current = True # set this window to be current window
window.next # the next window
window.prev # the previous window
window.close() # close this window
window.close_others() # close all windows except this one
# Reload/back/forward a page
browser.reload()
browser.back()
browser.forward()
# get page tile /page content /url
browser.title
browser.html
browser.url
# change Browser User-Agent
b = Browser(user_agent="Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)")
# Finding elements,returns a list with the found elements
browser.find_by_css('h1')
browser.find_by_xpath('//h1')
browser.find_by_tag('h1')
browser.find_by_name('name')
browser.find_by_text('Hello World!')
browser.find_by_id('firstheader')
browser.find_by_value('query')
# get element
first_found = browser.find_by_name('name').first
last_found = browser.find_by_name('name').last
second_found = browser.find_by_name('name')[1]
# Get value of an element
browser.find_by_css('h1').first.value
# Clicking links,return the first link
browser.click_link_by_href('http://www.the_site.com/my_link')
browser.click_link_by_partial_href('my_link')
browser.click_link_by_text('my link')
browser.click_link_by_partial_text('part of link text')
browser.click_link_by_id('link_id')
# element is visible or invisible
browser.find_by_css('h1').first.visible
# Verifying if element has a className
browser.find_by_css('.content').first.has_class('content')
# click button
browser.find_by_name('send').first.click()
browser.find_link_by_text('my link').first.click()
# Mouse
browser.find_by_tag('h1').mouse_over()
browser.find_by_tag('h1').mouse_out()
browser.find_by_tag('h1').click()
browser.find_by_tag('h1').double_click()
browser.find_by_tag('h1').right_click()
# Mouse drag and drop
draggable = browser.find_by_tag('h1')
target = browser.find_by_css('.container')
draggable.drag_and_drop(target)
# Interacting with forms
browser.fill('query', 'my name')
browser.attach_file('file', '/path/to/file/somefile.jpg')
browser.choose('some-radio', 'radio-value')
browser.check('some-check')
browser.uncheck('some-check')
browser.select('uf', 'rj')
# screenshot
browser.driver.save_screenshot('your_screenshot.png')
# 看不太懂
# trigger JavaScript events, like KeyDown or KeyUp, you can use the type method.
browser.type('type', 'typing text')
'''
If you pass the argument slowly=True to the type method you can interact with the page on every key pressed. Useful for
'''
# testing field's auto completion (the browser will wait until next iteration to type the subsequent key).
for key in browser.type('type', 'typing slowly', slowly=True):
pass # make some assertion here with the key object :)
# You can also use type and fill methods in an element:
browser.find_by_name('name').type('Steve Jobs', slowly=True)
browser.find_by_css('.city').fill('San Francisco')
# Dealing with HTTP status code and exceptions
browser.visit('http://cobrateam.info')
browser.status_code.is_success() # True
browser.status_code == 200 # True
browser.status_code.code #
# try:
# browser.visit('http://cobrateam.info/i-want-cookies')
# except HttpResponseError, e:
# print "Oops, I failed with the status code %s and reason %s" % (e.status_code, e.reason)
# test
# Cookies manipulation
browser.cookies.add({'whatever': 'and ever'}) # add a cookie
browser.cookies.all() # retrieve all cookies
browser.cookies.delete('mwahahahaha') # deletes the cookie 'mwahahahaha'
browser.cookies.delete('whatever', 'wherever') # deletes two cookies
browser.cookies.delete() # deletes all cookies
# Frames, alerts and prompts
# Using iframes,You can use the get_iframe method and the with statement to interact with iframes. You can pass the
# iframe's name, id, or index to get_ifram
with browser.get_iframe('iframemodal') as iframe:
iframe.do_stuff()
# Chrome support for alerts and prompts is new in Splinter 0.4.Only webdrivers (Firefox and Chrome) has support for
# alerts and prompts.
alert = browser.get_alert()
alert.text
alert.accept()
alert.dismiss()
prompt = browser.get_alert()
prompt.text
prompt.fill_with('text')
prompt.accept()
prompt.dismiss()
# use the with statement to interacte with both alerts and prompts
with browser.get_alert() as alert:
alert.do_stuff()
# Executing javascript
browser.execute_script("$('body').empty()")
browser.evaluate_script("4+4") == 8
# Matchers
browser = Browser()
browser.visit('https://splinter.readthedocs.io/')
browser.is_text_present('splinter') # True
browser.is_text_present('splinter', wait_time=10) # True, using wait_time
browser.is_not_present('text not present') # True
browser.is_element_present_by_css('h1')
browser.is_element_present_by_xpath('//h1')
browser.is_element_present_by_tag('h1')
browser.is_element_present_by_name('name')
browser.is_element_present_by_text('Hello World!')
browser.is_element_not_present_by_id('firstheader')
browser.is_element_not_present_by_value('query')
browser.is_element_present_by_value('query', wait_time=10)
#scroll 滑动屏幕
browser.evaluate_script('window.scrollTo(0,0)')
后期后整理更多的API
小程序常用API介绍
小程序常用API接口 wx.request https网络请求 wx.request({ url: 'test.php', //仅为示例,并非真实的接口地址 method:"GET&qu ...
appium 常用api介绍(2)
前言:接着上一篇继续讲常用的一些api 参考博文:http://blog.csdn.net/bear_w/article/details/50330565 1.send_keys send_keys( ...
appium 常用api介绍(1)
前言:android手机大家都很熟悉,操作有按键.触摸.点击.滑动等,各种操作方法可以通过api的方法来实现. 参考博文:http://blog.csdn.net/bear_w/article/det ...
java===字符串常用API介绍(转)
本文转自:http://blog.csdn.net/crazy_kid_hnf/article/details/55102861 字符串基本操作 1.substring(from,end)(含头不含尾 ...
java-org.dom4j常用api介绍
//导入必要的包 import org.dom4j.Document;//Document文档类 import org.dom4j.Element//元素节点类 import org.dom4j.QN ...
selenium2常用API介绍
我们模拟web操作都是基于元素来操作的,我们首先要先确定元素,然后这个元素下对应的方法就可以看WebElement的方法. 1.点击操作 WebElement button=driver.findEl ...
APPIUM 常用API介绍(3)
1.send_keys send_keys(self, *value): Simulates typing into the element[在元素中模拟输入(开启appium自带的输入法并配置了ap ...
常用ArcGIS for Silverlight 开发API介绍
1.API介绍 2.Map对象 3.Layer对象 4.Symbol对象 5.Task对象
随机推荐
一个js简单的日历显示效果的函数
用一个函数简单的实现一个月份的日历,效果如下: 这个日历效果有高亮显示,我实现的思维比较简单. 我把上面的日历效果用表格table生成,分成两个部分. 第一个部分:就是前面的第一排,我用一行
ashx
一般处理程序(HttpHandler)是·NET众多web组件的一种,ashx是其扩展名.一个httpHandler接受并处理一个http请求,类比于Java中的servlet.类比于在Java中需要 ...
Hadoop blocks
一In cases where the last record in a block is incomplete, the input split includes location informat ...
[python]字符串方法
字符串的方法及注释 字符串的方法及注释 capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 cente ...
emacs window版环境配置(设置默认的.emacs文件,指向自定义.emacs达到自定义home的目的)
1.下载解压包 下载地址 ,下载之后我是直接解压到E:\emacs中的,E:\emacs中就有bin,libexec…等文件; 2.点击bin中的addpm.exe文件进行安装emacs; 3.就会 ...
STM32F407VG (三)ADC
12位ADC是一种逐次逼近型模拟数字转换器. 它有多达19个通道,可測量16个外部和2个内部信号源和VBAT通道.各通道的A/D转换能够单次.连续.扫描或间断模式运行. ADC的结果能够左对齐或右对齐 ...
selenium 学习笔记 ---新手学习记录(5) 问题总结(java)
1.今天遇到个奇葩问题,iframe有两个id相同的(如下图) 使用driver.switchTo().frame(“frmLinkPage1”);这个无法使用了. 后来改用driver.switch ...
5.Hibernate实现全套增删改查和ajax异步分页
1.1 创建如下oracle数据库脚本 drop sequence seq_stu; create sequence SEQ_STU minvalue maxvalue start increment ...
typescript中的泛型
泛型:软件工程中,我们不仅要创建一致的定义良好的API,同时也要考虑可重用性. 组件不仅能够支持当前的数据类型,同时也能支持未来的数据类型,这在创建大型系统时为你提供了十分灵活的功能. 在像C#和Ja ...
【bug记录】OS Lab4 踩坑记
OS Lab4 踩坑记 Lab4在之前Lab3的基础上,增加了系统调用,难度增加了很多.而且加上注释不详细,开玩笑的指导书,自己做起来困难较大.也遇到了大大小小的bug,调试了一整天. 本文记录笔者在 ...