python3 selenuim PC端使用chrome模擬手機進行H5自動化

狀況說明:初次在作PC端使用chrome進行H5自動化測試時,覺得和app端自動化同樣使用click()就能夠對按鈕進行點擊,找了好幾天也沒有找到解決方法,有些人說是工程問題,有些人是使用微信進行H5測試,終於找到一篇文章介紹使用TouchAction模擬移動端觸摸操做。html

環境:python

win10+python3+pycharm+webdriver 2.40+chrome  68.0.3440.75web

問題:chrome

使用chrome模擬手機進行H5自動化,某些button按鈕使用click()沒法點擊微信

解決方法:app

使用selenuim庫裏TouchAction模擬鼠標在手機上實現觸屏點擊測試

頁面狀況:ui

deviceName查看處:spa

 

代碼:orm

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.touch_actions import TouchActions

#設置chrome爲手機瀏覽模式,deviceName就是手機型號
mobileEmulation = {'deviceName': 'iPhone 5'}
options = webdriver.ChromeOptions()
options.add_experimental_option('mobileEmulation', mobileEmulation)
driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options)

driver.get("http://ip/my.html")
#輸入帳號
driver.find_element_by_id("username").clear()
driver.find_element_by_id("username").send_keys("username")
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys("passwd")
sleep(3)
#點擊登陸按鈕
doc = driver.find_element_by_id("login")
TouchActions(driver).tap(doc).perform()
sleep(3)driver.quit()
相關文章
相關標籤/搜索