使用python selenium TouchAction模拟实现lCalendar滚动和点击效果
来源:互联网 发布:淘宝有好货报名入口 编辑:程序博客网 时间:2024/06/06 02:23
在做移动端页面自动化测试时,遇到了一个用Icanlendar实现的地区选择框。不同于PC所用的select,这个需要通过控制手指滑动的幅度来选择不同的地区。通过翻阅文档,我发现可以用selenium的TouchAction模拟用户在移动端的操作。TouchAction的文档地址:https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.touch_actions.html
示例代码:
def testWxLeaderSubmissionCreate(): driver.get("http://10.0.11.129/index.php/mLeaderSubmission/create/leaderTaskId/1755/fUserId/22930") quotation = driver.find_element_by_name("LeaderSubmission[quotation]") quotation.send_keys("111") contact = driver.find_element_by_name("LeaderSubmission[contacts]") contact.send_keys("fanTest002") phone = driver.find_element_by_name("LeaderSubmission[phone]") phone.clear()# phone.send_keys("13699269673") description = driver.find_element_by_name("LeaderSubmission[description]") description.send_keys("页面自动化测试") #driver.execute_script('$(\"input[name=area_info]\").removeAttr(\"readonly\");') area_info = driver.find_element_by_name("area_info") area_info.click() time.sleep(2) action = webdriver.TouchActions(driver) #touch action参见:https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.touch_actions.html source = driver.find_elements_by_class_name('tooth')[1] print(source.text) action.scroll_from_element(source,0,100)#滚动ICalendar插件中的选项 action.perform() time.sleep(3) action = webdriver.TouchActions(driver)#这个一定要重新再绑定一次,因为perform方法执行的时候就把action 给fire了 action.tap(driver.find_element_by_class_name('larea_finish')).perform() time.sleep(2) #area_info.send_keys("辽宁省,大连市,中山区") #driver.execute_script('$("input[name=\'LeaderSubmission[area_id]\']") .attr("type","text");') #area_id = driver.find_element_by_name("LeaderSubmission[area_id]") #area_id.send_keys("6,108,1532") area_info.submit(); print('微信端测试成功') print(driver.current_url)testWxLeaderSubmissionCreate()
Icalendar示例图片:
最后提醒大家一点,对于移动端进行页面测试的时候,一定要开网页的手机模拟器,否则一些代码是没办法起作用的哦。
开网页的手机模拟器的代码如下:
import timeimport osfrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import Selectfrom selenium.webdriver.chrome.options import Optionsmobile_emulation = { #"deviceName": "Apple iPhone 3GS" #"deviceName": "Apple iPhone 4" #"deviceName": "Apple iPhone 5" #"deviceName": "Apple iPhone 6" #"deviceName": "Apple iPhone 6 Plus" #"deviceName": "BlackBerry Z10" #"deviceName": "BlackBerry Z30" #"deviceName": "Google Nexus 4" #"deviceName": "Google Nexus 5" #"deviceName": "Google Nexus S" #"deviceName": "HTC Evo, Touch HD, Desire HD, Desire" #"deviceName": "HTC One X, EVO LTE" #"deviceName": "HTC Sensation, Evo 3D" #"deviceName": "LG Optimus 2X, Optimus 3D, Optimus Black" #"deviceName": "LG Optimus G" #"deviceName": "LG Optimus LTE, Optimus 4X HD" #"deviceName": "LG Optimus One" #"deviceName": "Motorola Defy, Droid, Droid X, Milestone" #"deviceName": "Motorola Droid 3, Droid 4, Droid Razr, Atrix 4G, Atrix 2" #"deviceName": "Motorola Droid Razr HD" #"deviceName": "Nokia C5, C6, C7, N97, N8, X7" #"deviceName": "Nokia Lumia 7X0, Lumia 8XX, Lumia 900, N800, N810, N900" #"deviceName": "Samsung Galaxy Note 3" #"deviceName": "Samsung Galaxy Note II" #"deviceName": "Samsung Galaxy Note" #"deviceName": "Samsung Galaxy S III, Galaxy Nexus" #"deviceName": "Samsung Galaxy S, S II, W" #"deviceName": "Samsung Galaxy S4" #"deviceName": "Sony Xperia S, Ion" #"deviceName": "Sony Xperia Sola, U" #"deviceName": "Sony Xperia Z, Z1" #"deviceName": "Amazon Kindle Fire HDX 7″" #"deviceName": "Amazon Kindle Fire HDX 8.9″" #"deviceName": "Amazon Kindle Fire (First Generation)" #"deviceName": "Apple iPad 1 / 2 / iPad Mini" #"deviceName": "Apple iPad 3 / 4" #"deviceName": "BlackBerry PlayBook" #"deviceName": "Google Nexus 10" #"deviceName": "Google Nexus 7 2" #"deviceName": "Google Nexus 7" #"deviceName": "Motorola Xoom, Xyboard" #"deviceName": "Samsung Galaxy Tab 7.7, 8.9, 10.1" #"deviceName": "Samsung Galaxy Tab" #"deviceName": "Notebook with touch" "deviceName": "iPhone 6" # Or specify a specific build using the following two arguments #"deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 }, #"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }}# Define a variable to hold all the configurations we wantchrome_options = webdriver.ChromeOptions()# Add the mobile emulation to the chrome options variablechrome_options.add_experimental_option("mobileEmulation", mobile_emulation)# Create driver, pass it the path to the chromedriver file and the special configurations you want to rundriver = webdriver.Chrome(executable_path="你存放chromedriver.exe的目录绝对路径", chrome_options=chrome_options)
配置好模拟器就开始写测试代码了。这次的页面自动化测试代码编写就到这里完成了。希望对各位小伙伴们能有所帮助。
最后附上TouchAction的英文文档(转载自https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.touch_actions.html,方便看不了的小伙伴):
selenium.webdriver.common.touch_actions
The Touch Actions implementation
class selenium.webdriver.common.touch_actions.TouchActions(driver)
Generate touch actions. Works like ActionChains; actions are stored in the TouchActions object and are fired with perform().
Creates a new TouchActions object.
Args :
driver: The WebDriver instance which performs user actions. It should be with touchscreen enabled.
double_tap(on_element)
Double taps on a given element.
Args : on_element: The element to tap.
flick(xspeed, yspeed)
Flicks, starting anywhere on the screen.
Args :
xspeed: The X speed in pixels per second.
yspeed: The Y speed in pixels per second.
flick_element(on_element, xoffset, yoffset, speed)
Flick starting at on_element, and moving by the xoffset and yoffset with specified speed.
Args :
on_element: Flick will start at center of element.
xoffset: X offset to flick to.
yoffset: Y offset to flick to.
speed: Pixels per second to flick.
long_press(on_element)
Long press on an element.
Args :
on_element: The element to long press.
move(xcoord, ycoord)
Move held tap to specified location.
Args :
xcoord: X Coordinate to move.
ycoord: Y Coordinate to move.
perform()
Performs all stored actions.
release(xcoord, ycoord)
Release previously issued tap ‘and hold’ command at specified location.
Args :
xcoord: X Coordinate to release.
ycoord: Y Coordinate to release.
scroll(xoffset, yoffset)
Touch and scroll, moving by xoffset and yoffset.
Args :
xoffset: X offset to scroll to.
yoffset: Y offset to scroll to.
scroll_from_element(on_element, xoffset, yoffset)
Touch and scroll starting at on_element, moving by xoffset and yoffset.
Args :
on_element: The element where scroll starts.
xoffset: X offset to scroll to.
yoffset: Y offset to scroll to.
tap(on_element)
Taps on a given element.
Args :
on_element: The element to tap.
tap_and_hold(xcoord, ycoord)
Touch down at given coordinates.
Args :
xcoord: X Coordinate to touch down.
ycoord: Y Coordinate to touch down.
- 使用python selenium TouchAction模拟实现lCalendar滚动和点击效果
- python selenium的模拟点击,模拟滑动等操作
- python爬虫 使用selenium+phontomjs 模拟点击输入 获取东航加载后的源码 机票价格
- js实现图片的点击滚动效果
- python+selenium 点击单选框-radio的实现
- selenium+python使用js点击元素
- 使用ScrollView实现滚动效果
- 使用ScrollView实现滚动效果
- 使用 HorizontalScrollView 实现水平滚动,并点击有相应的反应效果
- python selenium,PhantomJS运用 抓取滚动条滚动加载的页面, js动作操作,模拟登陆
- 模拟 手机QQ 点击头部图片 使其变成半透明 使用一张图片实现此效果
- python+Selenium模拟登录
- JavaScript中用Jquery实现左右点击滚动效果
- 点击页面上某个元素实现动画的滚动效果
- 纯CSS实现点击滚动翻页的效果
- python+selenium实现登录账户后自动点击
- 使用Python来模拟鼠标的点击
- 使用Python来模拟鼠标的点击
- 1.开始我的整理之路
- 互联网+助力恒德智能装备远程自动化控制
- Python Web开发
- 什么时候使用CountDownLatch
- 每日一练 20171110
- 使用python selenium TouchAction模拟实现lCalendar滚动和点击效果
- 准备报考信息系统项目管理工程师中级职称
- SQLZOO习题答案 The nobel table can be used to practice more SUM and COUNT functions./zh
- 修改爬虫的HTTP请求
- 关于Visual Studio "当前不会命中断点.还没有为该文档加载任何符号"的解决方法
- 执行 composer require 出现Warning: This development build of composer is over 60 days old.
- NOIp(lus)2017总结
- MySQL单机多实例配置(转载http://skypegnu1.blog.51cto.com/8991766/1441188)
- Struts2学习笔记-<s:append>与<s:merge>标签