当前位置:网站首页>Selenium WebDriver的高级特性
Selenium WebDriver的高级特性
2022-06-29 18:29:00 【是乔乔啊】
键盘与鼠标事件
通过WebDriver的ActionChains类实现。



键盘事件
通过ActionChains类,模拟同时按下shift+n键。
ActionChains(driver).\
key_down(Keys.SHIFT).\
send_keys('n').\
key_up(Keys.SHIFT).perform()
鼠标事件
move_to_element()可以将光标从当前位置移动到指定位置。
age_field = driver.find_element_by_id("age")
ActionChains(self.driver).move_to_element(age_field).perform()
双击操作
double_click方法实现双击。
box = driver.find_element_by_tag_name("div")
ActionChains(driver).double_click(box).perform()
鼠标拖动
通过drag_and_drop()实现鼠标的拖放操作。
source = driver.find_element_by_id("draggable")
target = driver.find_element_by_id("droppable")
ActionChains(self.driver).drag_and_drop(source,
target). perform()
调用JavaScript
WebDriver类包含的相关方法如下:
屏幕截图


当测试脚本找不到“promo_banner”元素时,程序就调用save_screenshot()方法来自动截屏,并以我们定义的图片文件名保存在指定的路径下。
try:
promo_banner_elem = driver.find_element_by_id("prom
o_banner")
self.assertEqual("Promotions", promo_banner_elem.te
xt)
except NoSuchElementException:
st = datetime.datetime.fromtimestamp(time.time()).
strftime('%Y%m%d_%H%M%S')
file_name = "main_page_missing_banner" + st + ".png
"
driver.save_screenshot(file__name)
raise
屏幕录制
Webdriver类中未集成屏幕录制的api方法。因此需要使用python中的Castro库。
Castro是基于跨平台屏幕录制工具Pyvnc2swf开发,使用VNC协议录制屏幕并生成SWF文件。
通过Castro,还可以实现远程机器的屏幕录制。
pip install Castro
from castro import Castro
class SearchProductsTest(unittest.TestCase):
def setUp(self):
self.screenCapture = Castro(filename="testSearchByCategory. swf")
self.screenCapture.start() # 开始录制
# 创建webdriver session,捕获页面元素坐标
...
def teardown(self):
self.driver.quit()
self.screenCapture.stop() # 退出录制
弹框处理
switch_to.window()方法可以获取弹窗的名称或句柄信息,切换到要操作的弹框上。
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.get(self.URL)
self.driver.maximize_window()
def test_popup_window(self):
driver=self.driver
parent_window_id = driver.current_window_handle
driver.switch_to.window("HelpWindow")
driver.close() # 关闭子窗口
driver.switch_to.window(parent_window_id) # 返回父窗口
操作cookies
WebDriver提供了读取、添加和删除cookies的方法。
def test_store_cookie(self):
store_cookie = driver.get_cookie("store")
self.assertEqual(None, store_cookie)
select_language.select_by_visible_text("French")
store_cookie = driver.get_cookie("store")['value'] # 通过传递cookie名称获取对应cookie的值
self.assertEqual("french", store_cookie)
边栏推荐
- markdown常用字体
- Shandong University project training (VIII) design rotation map entry page
- centos 7.5安装mysql 8.0.27----yum
- Adobe Premiere foundation - opacity (matte) (11)
- Fastdfs
- 2022.6.29-----leetcode.535
- Adobe Premiere foundation - time remapping (10)
- About microservices
- 1. 使用STM32CubeMX建立STM32G030C8T6项目工程
- 优雅书写Controller(参数验证+统一异常处理)
猜你喜欢

My first experience of remote office | community essay solicitation

jdbc认识上手

svg画圆路径动画

How to use idea?

Know that Chuangyu has helped the energy industry in asset management and was selected into the 2021 IOT demonstration project of the Ministry of industry and information technology

Fluent's MSH grid learning

JWT login authentication

About microservices

Source code installation mavros

These advantages of the Institute are really fragrant! The landing rate is still very high!
随机推荐
【日常训练】535. TinyURL 的加密与解密
postgis 生成 图形切割
The strategy of convertible bonds -- - (cake sharing, premium, forced redemption, downward revision, double low)
Adobe Premiere foundation - batch material import sequence - variable speed and rewind (recall) - continuous action shot switching - subtitle requirements (13)
Adobe Premiere Foundation - réglage du son (correction du volume, réduction du bruit, tonalité téléphonique, changement de hauteur, égaliseur de paramètres) (XVIII)
Leetcode 984. String without AAA or BBB (thought of netizens)
Sd6.23 summary of intensive training
Anaconda installs and configures jupyter notebook remote
[tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction
Application and practice of DDD in domestic hotel transaction -- Theory
Mac: MySQL 66 questions, 20000 words + 50 pictures!
6.29 simulation summary
Meta agent model can be migrated to resist attacks
garbage collector
Svg circle drawing path animation
WBF: new method of NMS post filter frame for detection task?
面试题 10.10. 数字流的秩
Adobe Premiere foundation - opacity (mixed mode) (XII)
Fastdfs
Interview question 10.10 Rank of digital stream