浏览器

1.火狐浏览器

br = webdriver.Firefox()
#最大化窗口
br.maximize_window()
br.get('http://baidu.com')

  

2.谷歌浏览器

br = webdriver.Chrome()
#最大化窗口
br.maximize_window()
br.get('http://baidu.com')

  

3.谷歌浏览器并且设置指定的下载目录,后面断言是否下载到本地

options=webdriver.ChromeOptions()
path=os.path.abspath("..")#表示当前所处的文件夹上一级文件夹的绝对路径
filepath=path+"\\PullFile"
prefs={'profile.default_content_settings.popups':0,'download.default_directory':filepath}
options.add_experimental_option('prefs',prefs) br=webdriver.Chrome(chrome_options=options)
br.maximize_window()
br.get(http://www.baidu.com)

  

4.PhantomJS浏览器,无界面的执行用例~

br = webdriver.PhantomJS()
br.maximize_window()
br.get("http://www.baidu.com")

  

4.1 谷歌无头浏览器,相当于PhantomJS

            chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu')
driver=webdriver.Chrome(chrome_options=chrome_options)
driver.maximize_window()

  

操作

1.点击动作

driver.find_element_by_id(Location_element).click()
driver.find_element_by_name(Location_element).click()
driver.find_element_by_xpath(Location_element).click()
driver.find_element_by_css_selector(Location_element).click()

2.输入动作,也可以作为上传文件使用

driver.find_element_by_id(Location_element).send_keys(Input_content)
driver.find_element_by_name(Location_element).send_keys(Input_content)
driver.find_element_by_xpath(Location_element).send_keys(Input_content)
driver.find_element_by_css_selector(Location_element).send_keys(Input_content)

 

3.选择select下拉框的值, Input_content给下拉框option的value值

Select(driver.find_element_by_xpath(Location_element)).select_by_value(Input_content)
Select(driver.find_element_by_name(Location_element)).select_by_value(Input_content)
Select(driver.find_element_by_id(Location_element)).select_by_value(Input_content)
Select(driver.find_element_by_css_selector(Location_element)).select_by_value(Input_content)

  

4.强制等待

time.sleep(2)

  

5.智能等待,一直等元素出来再操作, 30是设置的超时时间

driver.implicitly_wait(30)

  

6.悬浮操作

_ele_key = driver.find_element_by_id(Location_element)  #目标元素
ActionChains(driver).move_to_element(_ele_key).perform() #悬浮 _ele_key = driver.find_element_by_name(Location_element) #目标元素
ActionChains(driver).move_to_element(_ele_key).perform() #悬浮 _ele_key = driver.find_element_by_xpath(Location_element) #目标元素
ActionChains(driver).move_to_element(_ele_key).perform() #悬浮 _ele_key = driver.find_element_by_css_selector(Location_element)#目标元素
ActionChains(driver).move_to_element(_ele_key).perform()#悬浮

  

7.js弹窗确认/取消/输入值确认或取消

        #  js弹窗确认/取消/输入值确认或取消   Location_element指定义的操作,Input_content指需要输入的文本
def _prompts_js_key(self,driver,Location_element, Input_content): # 拿到页面alert
dialog_box = driver.switch_to_alert()
time.sleep(2)
# 这个判断给需要输入的提示框
if Location_element == u'确认' and Input_content != '':
text = str(Input_content)
dialog_box.send_keys(Input_content)
time.sleep(2)
dialog_box.accept()
time.sleep(2) if Location_element == u'取消' and Input_content != '':
dialog_box.dismiss()
time.sleep(2) #这两个判断是单纯只有确认 取消或确认的提示床
elif Location_element == u'确认' and Input_content == '':
dialog_box.accept()
time.sleep(2) elif Location_element == u'取消' and Input_content == '':
dialog_box.dismiss()
time.sleep(2) else:
pass

  

8.Autoit通过执行exe上传文件

paths=test._Autoit_file(exe_path)
time.sleep(1)
os.system(paths)
time.sleep(1)

  

9.切入iframe  #给iframe的元素(支持id name xpath...)

time.sleep(1)
driver.switch_to.frame(driver.find_element_by_xpath(Location_element)) #给iframe的元素
time.sleep(1)

  

10.切出iframe

driver.switch_to_default_content()

 

11.断言 预期和实际对比下

        def _find_value_key(self,driver,Positioning_mode,Location_element,Input_content,output):

            if Positioning_mode == self.Location_xpath:
#取实际元素的值
value = driver.find_element_by_xpath(Location_element).text
value = value.encode("utf-8")
value = str(value)
# text=str(text)
test=StringManipulation
Input_content = test._filter_value(Input_content) if isinstance(Input_content, int):
Input_content = str(Input_content)
Input_content = Input_content + 'X'
value = value + 'X' if isinstance(Input_content, float):
Input_content = str(Input_content)
Input_content = Input_content + 'X'
value = value + 'X' text = Input_content.encode("utf-8")
text = str(text) if value == text:
print(output,"-----TRUE")
ActualResults=('True')
return ActualResults
else:
print '--------------------------------------'
print ('预期结果',text)
print ('实际结果',value)
print('')
print ('实际结果和预期不匹配')
print '--------------------------------------'
driver.quit()
ActualResults='False'
return ActualResults
else:
print("find目前只用xpath定位方式")

  

12.回车操作

driver.find_element_by_id(Location_element).send_keys(Keys.ENTER)

  

13.清除文本框值操作

driver.find_element_by_id(Location_element).clear()

  

14.写个方法,在下载文件后,判断又没有下载到本地

# 得到目录文件
def file_name(self, file_dir):
for root, dirs, name in os.walk(file_dir):
return name # 查看是否有文件
def list_none(self, value):
if value:
return 'True'
else:
return 'False' # 断言是否下载文件成功
def find_file_key(self,driver,output): test_exc=StringManipulation._pull_file() files = self.file_name(test_exc)
list_value = self.list_none(files) if list_value == 'True': filename = test_exc + files[0]
os.remove(filename)
print(output,"-----TRUE")
ActualRresults='True'
print '下载成功,文件名是', files[0], '因为需要初始化,正在删除此文件.....'
return ActualRresults else:
print '指定的目录没有查询到下载的文件' ActualResults='False'
driver.quit()
return ActualResults

  

15.当时间选择框不可输入,那么改下js的写进去

        # 当时间控件不可输入时,需要用js去除removeAttribute属性再把值写进去
#Positioning_mode==定位方式,Location_element==元素,Input_content输入内容(时间)
def _time_js_input(self,driver,Positioning_mode,Location_element,Input_content): time.sleep(1)
elements=r"'"+Location_element+"'"
if Positioning_mode==self.Location_id:
jsa = "document.getElementById("+elements+").removeAttribute('readonly')"
driver.execute_script(jsa)
if isinstance(Input_content,float):
Input_content=int(Input_content)
Input_content=str(Input_content)
driver.find_element_by_id(Location_element).send_keys(Input_content)
time.sleep(1)
else:
driver.find_element_by_id(Location_element).send_keys(Input_content)
time.sleep(1)
else:
print('对于时间空间暂时先写id方法,后面用到其他在Underlyingkeyword.py维护')

  

后续有其他操作再补吧~

最新文章

  1. [转]RamDisk导致远程桌面客户端无法启动问题
  2. C# Exception 对象的属性
  3. 提供给开发者 10 款最好的 Python IDE
  4. php 开发最好的ide: PhpStorm
  5. TCP/IP详解学习笔记(3)-IP协议,ARP协议,RARP协议
  6. HTTP get、post请求
  7. SharePoint Framework 基于团队的开发(三)
  8. contos7搭建syslog服务端与客户端
  9. Linux压缩和解压缩
  10. 02: OpenStack
  11. cocos源码分析--用Sprite加载自定义着色器
  12. nginx gzip on 无效
  13. 从hash算法到java hashcode()
  14. Ios之网络编程NSURLConnection
  15. 20145101《Java程序设计》第6周学习总结
  16. Python之美[从菜鸟到高手]--2+2=5
  17. mac 必备工具
  18. codeforces 1096 题解
  19. 数据分析之pandas常见的数据处理(四)
  20. python request post上传文件额外注意点

热门文章

  1. P3649-[APIO2014]回文串【PAM】
  2. P4884-多少个1?【BSGS】
  3. ASP.NET Core 中间件的使用(三):全局异常处理机制
  4. 计算机网络-4-1-2-分类的IP地址
  5. HCNP Routing&Switching之路由引入导致的问题及解决方案
  6. Interrupted Exception异常可能没你想的那么简单!
  7. instanceof和类型转换
  8. 如何通过 Serverless 技术降低微服务应用资源成本?
  9. 解决nodejs的npm命令无反应的问题
  10. 2021-5-15 vj补题