web自动化测试脚本实现思路:
1.打开网站http://8.140.136.128/login
2.输入用户名、密码登陆
3.进入selenium-练习 表单元素操作页面
4.操作两个日期控件
5.进入selenium-练习 文件上传页面
6.操作文件上传
7.退出登陆
元素定位:
find_element_by_id 通过id属性定位
find_element_by_name 通过name属性定位
find_element_by_tag_name 通过标签名称等位
find_element_by_class_name 通过类名称定位
find_element_by_link_text 通过链接文本的完整内容定位
find_element_by_xpath
find_element_by_css_selector
find_element_by_partial_link_text
元素操作
send_keys() 输入文本
click() 点击
web自动化中的等待
1.sleep(timeout) 强制等待
暂停代码执行,等待timeout秒
优点:暴力,直接,简单
缺点:等待的时长不可能进行合理的设置
等待时间过长:页面已经加载完成,还在等待 --- 降低执行效率
等待时间太短:等待结束,页面还没有加载完成 --- 脚本缺乏健壮性
尽量不要使用!
2.implicitly_wait(timeout) 隐式等待
等待timeout秒,在timeout秒内,如果元素被定位到,则立即继续执行,超过sec秒,则抛出异常.
优点:只需要设置1次,且全局有效
3.WebDriverWait(driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None) 显示等待
until(self, method, message='')
1.自定义method
2.使用expected_conditions
selenium执行js脚本
driver.execute_script()
文件上传操作
1.<input type="file"> send_keys('image_src')
2.pywinauto python的win32自动化库
3.autoIt+python
翻车记录:
NoSuchElementException 没有定位到元素
解决思路:
1.定位器的写法是否错误
2.是否存在iframe
3.页面还没有加载完成
ElementClickInterceptedException 元素不能点击
解决思路:
1.等待要点击的元素能够被点击
2.通过js去点击
ElementNotInteractableException 元素不可见
解决思路:
1.操作的元素不可见,需要展开下来框