网站首页 > 基础教程 正文
一、前言
最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新!
这是python+selenium实现Web自动化第四篇博文
二、Selenium之-文件上传
通过input标签实现的上传功能,可以将其看作是一个输入框,即通过send_keys()指定本地文件路径的方式实现文件上传。
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Time : 2020/4/15
@License : (C)Copyright 2017-2019, Micro-Circle
@Desc : None
"""
from selenium import webdriver
import os
driver = webdriver.Chrome('../tools/chromedriver.exe')
file_path = "file:///" + os.path.abspath('upfile.html')
driver.get(file_path)
# 定位上传按钮的位置
driver.find_element_by_name('file').send_keys(os.path.abspath('upfile.txt'))
driver.quit()
四、Selenium之-Cookie操作
网站为了辨别用户身份、进行 session 跟踪而存储在用户本地终端上的数据,也可以叫做浏览器缓存。webdriver 对 cookie 的常用操作有添加、删除、读取。
(1) get_cookies()-----获得所有的 cookie 信息
(2) get_cookie(name)-----活得 key 值为 name 的 cookie 的信息
(3) add_cookie(cookie_dict)----添加 cookie。"cookie_dict" 指字典对象,必须有 name 和 value 值(4) delete_cookie(name,optionsString):删除cookie信息。“name”是要删除的cookie的名称,“optionsString”是该cookie的选项,目前支持的选项包括“路径”,“域”(5) delete_all_cookies()----删除所有 cookie 信息
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Time : 2020/4/15
@License : (C)Copyright 2017-2019, Micro-Circle
@Desc : None
"""
from selenium import webdriver
from time import sleep
base_url = 'https://www.baidu.com/'
browser = webdriver.Chrome('../tools/chromedriver.exe')
browser.get(base_url)
# 1. 获取 cookie 信息
cookies = browser.get_cookies()
print(cookies)
sleep(2)
browser.quit()
# 2. cookie 写入
browser.add_cookie(
{
'name': 'add-cookie',
'value': 'add-cookie-value'
}
)
# 遍历cookies打印cookie信息
for cookie in browser.get_cookies():
print("%s ---> %s" % (cookie['name'], cookie['value']))
sleep(2)
browser.quit()
五、Selenium之-调用 JavaScript
?对于 webdriver 中无法操作的动作(例如:滚动浏览器的侧边栏),可以调用 webdriver 进行浏览器的控制。webdriver 提供了execute_script()方法来执行 JavaScript 代码。
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Time : 2020/4/15
@Contact : 软件测试技术群:695458161 免费领取测试进阶资料!
@License : (C)Copyright 2017-2019, Micro-Circle
@Desc : None
"""
from selenium import webdriver
from time import sleep
base_url = 'https://www.baidu.com'
browser = webdriver.Chrome('../tools/chromedriver.exe')
browser.get(base_url)
# window.scrollTo()方法用于设置浏览器窗口滚动条的水平和垂直位置。方法的第一个参数表示水平的左间距,第二个参数表示垂直的上边距。
browser.set_window_size(500, 500)
browser.find_element_by_id('kw').send_keys('百度')
browser.find_element_by_id('su').click()
sleep(2)
# 通过javascript设置浏览器窗口的滚动条位置
js = "window.scrollTo(100, 450);"
browser.execute_script(js)
sleep(2)
browser.quit()
六、Selenium之-窗口截图
自动化用例是由程序去执行的,因此有时候打印的错误信息并不十分明确。如果在脚本执行出错的时候能对当前窗口截图保存,那么通过图片就可以非常直观地看出出错的原因。webdriver 提供了截图函数 get_screenshot_as_file() 来截取当前窗口。
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Time : 2020/4/15
@Contact : 软件测试技术群:695458161 免费领取测试进阶资料!
@License : (C)Copyright 2017-2019, Micro-Circle
@Desc : None
"""
from selenium import webdriver
from time import sleep
base_url = 'http://www.baidu.com/'
browser = webdriver.Chrome('../tools/chromedriver.exe')
browser.get(base_url)
browser.find_element_by_id('kw').send_keys('python selenium')
browser.find_element_by_id('su').click()
sleep(2)
# 截取当前窗口并指定报错截图的位置
# browser.get_screenshot_as_file('ScreenShot/14_screenShot.jpg')
browser.get_screenshot_as_file('ScreenShot/14_screenShot.png')
browser.quit()
七、Selenium之-关闭窗口
close() 关闭单个窗口
quit() 关闭所有窗口
猜你喜欢
- 2024-11-26 工作中,前端开发要看项目,怎么查看别人的js项目代码
- 2024-11-26 Cookie 在爬虫中的应用
- 2024-11-26 55个JS代码让你轻松当大神
- 2024-11-26 操作cookie
- 2024-11-26 9个原生js库助力前端开发,你都用过吗?
- 2024-11-26 Selenium4+Python3系列(七) Iframe、常见控件、JS、Cookie操作
- 2024-11-26 浅谈JavaScript的用处
- 2024-11-26 Web安全:XSS怎么实现?Cookie 劫持如此简单,你的网站安全吗?
- 2024-11-26 Cookies和Session的区别和理解
- 2024-11-26 cookie是什么?有什么用?cookie详解,一篇文章彻底搞懂cookie
- 06-09Socioeconomic growth goals high on meetings' agenda
- 06-09Cities Along Middle Reaches of Yangtze River Agree on 63 Cooperation Items
- 06-09Scientists to make flag flutter on moon
- 06-09CBN丨Foreign-funded institutions bullish on Chinese assets
- 06-09Full Text: Joint Statement between the People's Republic of China and the French Republic on Climate Change on the occasion of the Tenth Anniversary of the Paris Agreement
- 06-092022年底总结,温暖和激励自己的文案
- 06-09百度实时推送代码解决方案(百度实时推送工具)
- 06-09PHP漏洞之跨网站请求伪造(php跨站脚本攻击)
- 最近发表
-
- Socioeconomic growth goals high on meetings' agenda
- Cities Along Middle Reaches of Yangtze River Agree on 63 Cooperation Items
- Scientists to make flag flutter on moon
- CBN丨Foreign-funded institutions bullish on Chinese assets
- Full Text: Joint Statement between the People's Republic of China and the French Republic on Climate Change on the occasion of the Tenth Anniversary of the Paris Agreement
- 2022年底总结,温暖和激励自己的文案
- 百度实时推送代码解决方案(百度实时推送工具)
- PHP漏洞之跨网站请求伪造(php跨站脚本攻击)
- ThinkPHP后台入口地址查找(thinkphp build.php)
- PHP新手如何提高代码质量(php新手如何提高代码质量的方法)
- 标签列表
-
- jsp (69)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- pythonif (86)
- location.href (69)
- dockerexec (65)
- tail-f (79)
- queryselectorall (63)
- location.search (79)
- bootstrap教程 (74)
- deletesql (62)
- linuxgzip (68)
- 字符串连接 (73)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)