test-splinter.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. @Auther :liuyuqi.gov@msn.cn
  5. @Time :2018/4/11 15:31
  6. @File :test-phantomjs.py
  7. '''
  8. # from splinter import Browser
  9. import splinter
  10. url_home="http://blog.yoqi.me/archives/4387"
  11. def t1():
  12. browser = splinter.Browser("phantomjs", executable_path="D:/Program-Files/phantomjs-2.1.1-windows/bin/phantomjs.exe")
  13. browser.visit(url_home)
  14. print(browser.title)
  15. def t2():
  16. browser=splinter.Browser("chrome",executable_path="C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
  17. browser.visit(url_home)
  18. print(browser.title)
  19. def t22():
  20. browser=splinter.Browser("firefox")
  21. browser.visit(url_home)
  22. print(browser.title)
  23. def t3():
  24. b = splinter.Browser('chrome')
  25. url = 'https://kyfw.12306.cn/otn/leftTicket/init'
  26. b.visit(url) #访问网址
  27. first_found = b.find_by_id('login_user').click()
  28. b.fill('loginUserDTO.user_name', '12306账号')
  29. b.fill('userDTO.password', '12306密码')
  30. input()
  31. sec_found = b.find_by_id('selectYuding').click()
  32. b.cookies.add({'_jc_save_fromStation': '%u4E0A%u6D77%2CSHH'})
  33. b.cookies.add({'_jc_save_toStation': '%u6D1B%u9633%2CLYF'})
  34. b.cookies.add({'_jc_save_fromDate': '2017-02-17'})
  35. b.cookies.add({'_jc_save_toDate': '2017-02-19'})
  36. b.reload()
  37. third_found = b.find_by_text(u'查询').click()
  38. b.find_by_text(u'预订')[4].click()
  39. def t4():
  40. with splinter.Browser() as browser:
  41. url = "https://www.baidu.com"
  42. browser.visit(url)
  43. browser.fill('q', 'splinter - python acceptance testing for web applications')
  44. # Find and click the 'search' button
  45. button = browser.find_by_name('btnG')
  46. # Interact with elements
  47. button.click()
  48. if browser.is_text_present('splinter.readthedocs.io'):
  49. print("Yes, the official website was found!")
  50. else:
  51. print("No, it wasn't found... We need to improve our SEO techniques")
  52. # browser type
  53. browser = splinter.Browser('chrome')
  54. browser = splinter.Browser('firefox')
  55. browser = splinter.Browser('zope.testbrowser')
  56. # Managing Windows
  57. browser.windows # all open windows
  58. browser.windows[0] # the first window
  59. browser.windows["window_name"] # the window_name window
  60. browser.windows.current # the current window
  61. browser.windows.current = browser.windows[3] # set current window to window 3
  62. # splinter api不提供但是可以通过其他来搞定的,比如通过driver来设置window的大小。
  63. browser.driver.set_window_size(1600, 1000)
  64. window = browser.windows[0]
  65. window.is_current # boolean - whether window is current active window
  66. window.is_current = True # set this window to be current window
  67. window.next # the next window
  68. window.prev # the previous window
  69. window.close() # close this window
  70. window.close_others() # close all windows except this one
  71. # Reload/back/forward a page
  72. browser.reload()
  73. browser.back()
  74. browser.forward()
  75. # get page tile /page content /url
  76. browser.title
  77. browser.html
  78. browser.url
  79. # change Browser User-Agent
  80. b = splinter.Browser(user_agent="Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)")
  81. # Finding elements,returns a list with the found elements
  82. browser.find_by_css('h1')
  83. browser.find_by_xpath('//h1')
  84. browser.find_by_tag('h1')
  85. browser.find_by_name('name')
  86. browser.find_by_text('Hello World!')
  87. browser.find_by_id('firstheader')
  88. browser.find_by_value('query')
  89. # get element
  90. first_found = browser.find_by_name('name').first
  91. last_found = browser.find_by_name('name').last
  92. second_found = browser.find_by_name('name')[1]
  93. # Get value of an element
  94. browser.find_by_css('h1').first.value
  95. # Clicking links,return the first link
  96. browser.click_link_by_href('http://www.the_site.com/my_link')
  97. browser.click_link_by_partial_href('my_link')
  98. browser.click_link_by_text('my link')
  99. browser.click_link_by_partial_text('part of link text')
  100. browser.click_link_by_id('link_id')
  101. # element is visible or invisible
  102. browser.find_by_css('h1').first.visible
  103. # Verifying if element has a className
  104. browser.find_by_css('.content').first.has_class('content')
  105. # click button
  106. browser.find_by_name('send').first.click()
  107. browser.find_link_by_text('my link').first.click()
  108. # Mouse
  109. browser.find_by_tag('h1').mouse_over()
  110. browser.find_by_tag('h1').mouse_out()
  111. browser.find_by_tag('h1').click()
  112. browser.find_by_tag('h1').double_click()
  113. browser.find_by_tag('h1').right_click()
  114. # Mouse drag and drop
  115. draggable = browser.find_by_tag('h1')
  116. target = browser.find_by_css('.container')
  117. draggable.drag_and_drop(target)
  118. # Interacting with forms
  119. browser.fill('query', 'my name')
  120. browser.attach_file('file', '/path/to/file/somefile.jpg')
  121. browser.choose('some-radio', 'radio-value')
  122. browser.check('some-check')
  123. browser.uncheck('some-check')
  124. browser.select('uf', 'rj')
  125. # screenshot
  126. browser.driver.save_screenshot('your_screenshot.png')
  127. # 看不太懂
  128. # trigger JavaScript events, like KeyDown or KeyUp, you can use the type method.
  129. browser.type('type', 'typing text')
  130. '''
  131. If you pass the argument slowly=True to the type method you can interact with the page on every key pressed. Useful for
  132. '''
  133. # testing field's auto completion (the browser will wait until next iteration to type the subsequent key).
  134. for key in browser.type('type', 'typing slowly', slowly=True):
  135. pass # make some assertion here with the key object :)
  136. # You can also use type and fill methods in an element:
  137. browser.find_by_name('name').type('Steve Jobs', slowly=True)
  138. browser.find_by_css('.city').fill('San Francisco')
  139. # Dealing with HTTP status code and exceptions
  140. browser.visit('http://cobrateam.info')
  141. browser.status_code.is_success() # True
  142. browser.status_code == 200 # True
  143. browser.status_code.code # 200
  144. # try:
  145. # browser.visit('http://cobrateam.info/i-want-cookies')
  146. # except HttpResponseError, e:
  147. # print "Oops, I failed with the status code %s and reason %s" % (e.status_code, e.reason)
  148. # test
  149. # Cookies manipulation
  150. browser.cookies.add({'whatever': 'and ever'}) # add a cookie
  151. browser.cookies.all() # retrieve all cookies
  152. browser.cookies.delete('mwahahahaha') # deletes the cookie 'mwahahahaha'
  153. browser.cookies.delete('whatever', 'wherever') # deletes two cookies
  154. browser.cookies.delete() # deletes all cookies
  155. # Frames, alerts and prompts
  156. # Using iframes,You can use the get_iframe method and the with statement to interact with iframes. You can pass the
  157. # iframe's name, id, or index to get_ifram
  158. with browser.get_iframe('iframemodal') as iframe:
  159. iframe.do_stuff()
  160. # Chrome support for alerts and prompts is new in Splinter 0.4.Only webdrivers (Firefox and Chrome) has support for
  161. # alerts and prompts.
  162. alert = browser.get_alert()
  163. alert.text
  164. alert.accept()
  165. alert.dismiss()
  166. prompt = browser.get_alert()
  167. prompt.text
  168. prompt.fill_with('text')
  169. prompt.accept()
  170. prompt.dismiss()
  171. # use the with statement to interacte with both alerts and prompts
  172. with browser.get_alert() as alert:
  173. alert.do_stuff()
  174. # Executing javascript
  175. browser.execute_script("$('body').empty()")
  176. browser.evaluate_script("4+4") == 8
  177. # Matchers
  178. browser = splinter.Browser()
  179. browser.visit('https://splinter.readthedocs.io/')
  180. browser.is_text_present('splinter') # True
  181. browser.is_text_present('splinter', wait_time=10) # True, using wait_time
  182. browser.is_not_present('text not present') # True
  183. browser.is_element_present_by_css('h1')
  184. browser.is_element_present_by_xpath('//h1')
  185. browser.is_element_present_by_tag('h1')
  186. browser.is_element_present_by_name('name')
  187. browser.is_element_present_by_text('Hello World!')
  188. browser.is_element_not_present_by_id('firstheader')
  189. browser.is_element_not_present_by_value('query')
  190. browser.is_element_present_by_value('query', wait_time=10)
  191. if __name__ == '__main__':
  192. t1()