selenium+python自动化84-chrome手机wap模式(登录淘宝页面)
转载自: https://www.cnblogs.com/yoyoketang/p/7942275.html
前言
chrome手机wap模式登录淘宝页面,点击验证码无效问题解决。
切换到wap模式,使用TouchActions模块用tap方法触摸
我的环境
- chrome 62
- chromedriver 2.33
遇到的问题
1.登录手机版淘宝时候,验证码无法通过点击事件触发
[wap版淘宝](https://login.m.taobao.com/msg_login.htm?spm=0.0.0.0)
但是在F12 切换成手机模式后发现输入手机号后,手动操作是可以触发验证码的
Chrome手机模式
1.添加Options配置,设置成手机模式访问
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
url = "https://login.m.taobao.com/msg_login.htm?spm=0.0.0.0"
mobile_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"
}
options = Options()
options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(chrome_options=options)
driver.get(url)
2.这里的deviceName点开设备选项,选择一个手机设备名称
TouchAction
1.这里输入手机号后,用click点‘获取验证码’是无效的,这时候需要换个思维,用TouchActions里面的tap触摸方式去触发‘获取验证码’按钮
2.TouchAction里面的几个用法
class TouchAction(object):
def __init__(self, driver=None):
self._driver = driver
self._actions = []
def tap(self, element=None, x=None, y=None, count=1):
模拟手指触摸屏
def press(self, el=None, x=None, y=None):
短按:模拟手指按住一个元素,或者坐标
def long_press(self, el=None, x=None, y=None, duration=1000):
长按:模拟按住一个元素,或者坐标
def wait(self, ms=0):
按住元素后的等待时间
def move_to(self, el=None, x=None, y=None):
移动手指到另外一个元素,或者坐标,注意这里坐标不是绝对坐标,是偏移量
def release(self):
释放手指
def perform(self):
执行前面的动作
参考代码
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.touch_actions import TouchActions
from selenium.webdriver.chrome.options import Options
url = "https://login.m.taobao.com/msg_login.htm?spm=0.0.0.0"
# 设置成手机模式
mobile_emulation = {"deviceName":"iPhone 6"}
options = Options()
options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(chrome_options=options)
driver.get(url)
driver.find_element_by_id("username").send_keys("yoyoketang")
# 触摸事件
el = driver.find_element_by_id('getCheckcode')
TouchActions(driver).tap(el).perform()
TouchAction提供的其他方法:
double_tap(on_element) #双击
flick_element(on_element, xoffset, yoffset, speed) #从元素开始以指定的速度移动
long_press(on_element) #长按不释放
move(xcoord, ycoord) #移动到指定的位置
perform() #执行链中的所有动作
release(xcoord, ycoord) #在某个位置松开操作
scroll(xoffset, yoffset) #滚动到某个位置
scroll_from_element(on_element, xoffset, yoffset) #从某元素开始滚动到某个位置
tap(on_element) #单击
tap_and_hold(xcoord, ycoord) #某点按住
附:
selenium touchAction官方文档:
https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.touch_actions.html