TouchActions:模拟pc和h5的点击,滑动,拖拽,多点触控等多种手势操作
实现方法:new_action.scroll_from_element(a, 0, 10000).perform(),10000表示滑动的偏移量,如果不知道大小,尽量最大。
示例:百度页面-输入'灵题库'-百度一下,然后滑动页面并最大化窗口
from time import sleep
from selenium import webdriver
from selenium.webdriver import TouchActions
from selenium.webdriver.common.by import By
class TestTouchAction():
"""
百度页面,输入'灵题库'搜索,滑动页面,最大化窗口
"""
def setup(self):
option = webdriver.ChromeOptions()
option.add_experimental_option('w3c', False)
self.driver = webdriver.Chrome(options=option)
# 隐式等待
self.driver.implicitly_wait(5)
# 窗口最大化
self.driver.maximize_window()
self.driver.get('http://www.baidu.com')
def teardown(self):
self.driver.quit()
def test_touchaction(self):
"""
页面滑动
:return:
"""
self.driver.find_element(By.XPATH, '//*[@id="kw"]').send_keys('灵题库')
element_search = self.driver.find_element(By.XPATH, '//*[@id="su"]')
action = TouchActions(self.driver)
# 通过action点击百度一下
action.tap(element_search)
action.perform()
sleep(2)
# 百度首页到list页,页面变化之后,scroll_from_element报错,重新定位下输入框和action
element_input = self.driver.find_element(By.XPATH, '//*[@id="kw"]')
new_action = TouchActions(self.driver)
new_action.scroll_from_element(element_input, 0, 10000).perform()
网友评论