# 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

Splinter常用API介绍(转)的更多相关文章

  1. 小程序常用API介绍

    小程序常用API接口  wx.request https网络请求 wx.request({ url: 'test.php', //仅为示例,并非真实的接口地址 method:"GET&qu ...

  2. appium 常用api介绍(2)

    前言:接着上一篇继续讲常用的一些api 参考博文:http://blog.csdn.net/bear_w/article/details/50330565 1.send_keys send_keys( ...

  3. appium 常用api介绍(1)

    前言:android手机大家都很熟悉,操作有按键.触摸.点击.滑动等,各种操作方法可以通过api的方法来实现. 参考博文:http://blog.csdn.net/bear_w/article/det ...

  4. java===字符串常用API介绍(转)

    本文转自:http://blog.csdn.net/crazy_kid_hnf/article/details/55102861 字符串基本操作 1.substring(from,end)(含头不含尾 ...

  5. java-org.dom4j常用api介绍

    //导入必要的包 import org.dom4j.Document;//Document文档类 import org.dom4j.Element//元素节点类 import org.dom4j.QN ...

  6. java中FILE类常用API介绍

  7. selenium2常用API介绍

    我们模拟web操作都是基于元素来操作的,我们首先要先确定元素,然后这个元素下对应的方法就可以看WebElement的方法. 1.点击操作 WebElement button=driver.findEl ...

  8. APPIUM 常用API介绍(3)

    1.send_keys send_keys(self, *value): Simulates typing into the element[在元素中模拟输入(开启appium自带的输入法并配置了ap ...

  9. 四. 几个Promise常用API的介绍与使用

    四. 几个常用API的介绍与使用 1. Promise构造函数:Promise(excutor){} excutor函数:同步执行 (resolve, reject) => {} resolve ...

随机推荐

  1. python入门(十二):面向对象

    1.场景:玩过游戏.主人公,进入了一个场景,有10个小怪物是一样的.有攻击力,血(100格).如果小怪物有多个数值需要管理,小怪物的血量.小怪物出现在屏幕的地点. 可以使用字典来进行记录: {&quo ...

  2. BOOST_PREVENT_MACRO_SUBSTITUTION

    [BOOST_PREVENT_MACRO_SUBSTITUTION] 用于防止函数被macro替换的问题. 例如: 参考: 1.https://blog.csdn.net/yanxiangtianji ...

  3. Python列表的三种遍历(序号和值)的方法

    #-×-coding:utf-8-*- if _name_=='_main_': list=['html','js','css','python'] #方法1 print ‘遍历列表方法1’ for ...

  4. Locust 学习一 :初识

    之前就听过Locust是基于python的一款很好用的开源性能测试框架,一直没机会实践,正好这次项目上有个接口压测的小任务,就拿来练练手 安装:py -3 -m pip install locusti ...

  5. 2019-04(1)(Python学习)

    9.1 迭代器 创建迭代器的3种方法: 方法一: 容器对象添加 __iter__() 和 __next__() 方法(Python 2.7 中是 next()):__iter__() 返回迭代器对象本 ...

  6. 100-days: twenty-one

    Title: Not so fantastic(<口>极好的,棒的): can Japan end its love affair(喜爱,热爱) with plastic(塑料)? A : ...

  7. 【Spring源码解析】—— 结合SpringMVC过程理解IOC容器初始化

    关于IOC容器的初始化,结合之前SpringMVC的demo,对其过程进行一个相对详细的梳理,主要分为几个部分: 一.IOC的初始化过程,结合代码和debug过程重点说明 1. 为什么要debug? ...

  8. jQuery实现动态分割div

    转自:https://www.cnblogs.com/herd/p/6014848.html 演示地址:http://www.vfkjsd.cn/div/2/div.html

  9. Python中的计时器对象

    计时器对象用于特定时间运行的操作.往往被安排到特定的单独的线程上运行, 但是计时器初始化的时间间隔可能不是解释器实际执行操作时的实际时刻, 因为线程调度程序负责实际调度与计时器对象相对应的线程. Ti ...

  10. mysql约束以及数据库的修改

    一.约束 1.约束保证数据完整性和一致性. 2.约束分为表级约束和列级约束. (1)表级约束(约束针对于两个或两个以上的字段使用) (2)列级约束(针对于一个字段使用) 3.约束类型有: (1)NOT ...