selenium Webdriver自動化測試之手勢操作TouchAction 詳解


TouchAction,類似於ActionChains,ActionChains只是針對PC端程序鼠標模擬的一系列操作,對H5頁面操作是無效的。TouchAction可以對H5頁面操作,通過TouchAction可以實現點擊、滑動、拖拽、多點觸控,以及模擬手勢等各種操作。

關於 ActionChains的介紹可移步:  https://www.cnblogs.com/feng0815/p/8344120.html

手勢控制

1、按壓控件

方法:

  • press()

開始按壓一個元素或坐標點(x,y)。通過手指按壓手機屏幕的某個位置。

press(WebElement el, int x, int y)

press也可以接收屏幕的坐標(x,y)。

例:TouchAction(driver).press(x=0,y=308).release().perform()

除了press()方法之外,本例中還用到了別外兩個新方法。

  • release() 結束的行動取消屏幕上的指針。

  • Perform() 執行的操作發送到服務器的命令操作。

2、長按控件

方法:

  • longPress()

開始按壓一個元素或坐標點(x,y)。 相比press()方法,longPress()多了一個入參,既然長按,得有按的時間吧。duration以毫秒為單位。1000表示按一秒鍾。其用法與press()方法相同。

longPress(WebElement el, int x, int y, Duration duration)

例: 

  1. TouchAction action = new TouchAction(driver);
  2. action.longPress(names.get( 200),1000).perform().release();
  3. action.longPress( 200 ,200,1000).perform().release();

3、點擊控件

方法:

  • tap()

對一個元素或控件執行點擊操作。用法參考press()。

tap(WebElement el, int x, int y)

例:

  1.  TouchAction action = new TouchAction(driver);
  2. action.tap(names.get( 200)).perform().release();
  3.  action.tap( 200 ,200).perform().release();

4、移動

方法:

  • moveTo()

將指針(光標)從過去指向指定的元素或點。

movTo(WebElement el, int x, int y)

其用法參考press()方法。

例:

  1.  TouchAction action = new TouchAction(driver);
  2.  action.moveTo(names.get( 200)).perform().release();
  3.  action.moveTo( 200 ,200).perform().release();

5、暫停

方法:

  • wait()

暫停腳本的執行,單位為毫秒。

action.wait(1000);
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:chenshifeng
@file:test_TouchAction.py
@time:2020/10/17
"""

import time
from selenium import webdriver
from selenium.webdriver import TouchActions


class TestTouchAction():

    def setup_method(self):
        option = webdriver.ChromeOptions()
        option.add_experimental_option('w3c', False)
        self.driver = webdriver.Chrome(options=option)
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)

    def teardown_method(self):
        self.driver.quit()

    def test_touchaction_scrollbottom(self):
        self.driver.get("https://www.baidu.com/")
        el = self.driver.find_element_by_id('kw')
        el_search = self.driver.find_element_by_id('su')
        el.send_keys('selenium測試')
        action = TouchActions(self.driver)
        action.tap(el_search)  # 點擊
        action.perform()
        action.scroll_from_element(el, 0, 10000).perform()
        time.sleep(3)

end


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM