首页 技术 正文
技术 2022年11月9日
0 收藏 765 点赞 2,280 浏览 5469 个字
# Examplefrom splinter import Browserwith 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 typebrowser = Browser('chrome')browser = Browser('firefox')browser = Browser('zope.testbrowser')# Managing Windowsbrowser.windows              # all open windowsbrowser.windows[0]           # the first windowbrowser.windows["window_name"] # the window_name windowbrowser.windows.current      # the current windowbrowser.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 windowwindow.is_current = True     # set this window to be current windowwindow.next                  # the next windowwindow.prev                  # the previous windowwindow.close()               # close this windowwindow.close_others()        # close all windows except this one# Reload/back/forward a pagebrowser.reload()browser.back()browser.forward()# get page tile /page content /urlbrowser.titlebrowser.htmlbrowser.url# change Browser User-Agentb = Browser(user_agent="Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)")# Finding elements,returns a list with the found elementsbrowser.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 elementfirst_found = browser.find_by_name('name').firstlast_found = browser.find_by_name('name').lastsecond_found = browser.find_by_name('name')[1]# Get value of an elementbrowser.find_by_css('h1').first.value# Clicking links,return the first linkbrowser.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 invisiblebrowser.find_by_css('h1').first.visible# Verifying if element has a classNamebrowser.find_by_css('.content').first.has_class('content')# click buttonbrowser.find_by_name('send').first.click()browser.find_link_by_text('my link').first.click()# Mousebrowser.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 dropdraggable = browser.find_by_tag('h1')target = browser.find_by_css('.container')draggable.drag_and_drop(target)# Interacting with formsbrowser.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')# screenshotbrowser.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 exceptionsbrowser.visit('http://cobrateam.info')browser.status_code.is_success() # Truebrowser.status_code == 200 # Truebrowser.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 manipulationbrowser.cookies.add({'whatever': 'and ever'}) # add a cookiebrowser.cookies.all() # retrieve all cookiesbrowser.cookies.delete('mwahahahaha')  # deletes the cookie 'mwahahahaha'browser.cookies.delete('whatever', 'wherever')  # deletes two cookiesbrowser.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_iframwith 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.textalert.accept()alert.dismiss()prompt = browser.get_alert()prompt.textprompt.fill_with('text')prompt.accept()prompt.dismiss()# use the with statement to interacte with both alerts and promptswith browser.get_alert() as alert:    alert.do_stuff()# Executing javascriptbrowser.execute_script("$('body').empty()")browser.evaluate_script("4+4") == 8# Matchersbrowser = Browser()browser.visit('https://splinter.readthedocs.io/')browser.is_text_present('splinter')  # Truebrowser.is_text_present('splinter', wait_time=10)   # True, using wait_timebrowser.is_not_present('text not present')  # Truebrowser.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
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,092
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,568
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,416
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,189
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,825
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,908