您的位置:首页 > 编程语言 > Python开发

Selenium2+Python 自动化测试学习笔记(二)

2018-03-20 15:12 357 查看

1、警告框处理

   在WebDriver 中处理JavaScript 所生成的alert、confirm 以及prompt是很简单的。具体做法是使用switch_to_alert()方法定位到alert/confirm/prompt。然后使用text/accept/dismiss/send_keys按需进行操做。
l  text 返回alert/confirm/prompt中的文字信息。l  accept 点击确认按钮。l  dismiss 点击取消按钮,如果有的话。l  send_keys 输入值,这个alert\confirm没有对话框就不能用了,不然会报错。弹出框不能通过前端工具对其进行定位,可以通过switch_to_alert()方法接收这个弹窗。步骤:ActionChains 类所提供的move_to_element()鼠标悬停的使用,将鼠标悬停在“搜索”链接上然后弹出下拉菜单。在菜单中点击“搜索设置”按钮。
设置完成点击“保存设置”弹出警告框。
通过switch_to_alert()方法获取当前页上的警告框,accept()接受警告框。

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

driver=webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://www.baidu.com")

#mouse stop set link
link=driver.find_element_by_link_text("设置")

ActionChains(driver).move_to_element(link).perform()

#open search setting
driver.find_element_by_link_text('搜索设置').click()

#这里加个等待时间,不然会报错,给selenium个处理时间
time.sleep(3)

#save set
driver.find_element_by_xpath('//*[@id="gxszButton"]/a[1]').click()

#recieve pop_window
time.sleep(3)

driver.switch_to_alert().accept()

time.sleep(5)

driver.close()

2、三种等待方式

3、定位以及切换frame(iframe)

http://blog.csdn.net/huilan_same/article/details/52200586

4、多文件上传方法

http://blog.csdn.net/huilan_same/article/details/52439546

5、操作Cookie

      
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: