selenium 模拟手机浏览器操作 click点击/tap触摸 元素无效 的解决方法

我遇到的问题

  1. 获取到 登录按钮 的 xpath,且可以保证 xpath 正确无误
  2. 点击 登录按钮, 无法正常跳转到 登录成功页
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

mobile_emulation = {"deviceName": "iPhone X"}
options = Options()
options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://xxx.com")
#输入帐号密码
driver.find_element_by_xpath("//*[@id='email-login-input']/div/div[2]/input").send_keys("user")
driver.find_element_by_xpath("/html/body/div/div/div[2]/div[2]/div[2]/div[4]/div/div/div[2]/input").send_keys("password")
#点击登录按钮
driver.find_element_by_xpath("/html/body/div/div/div[2]/div[2]/div[2]/button").click()
#点击后,无任何反应
  1. 触摸 登录按钮, 无法正常跳转到 登录成功页
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from selenium.webdriver.common.touch_actions import TouchActions

mobile_emulation = {"deviceName": "iPhone X"}
options = Options()
options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://xxx.com")
#输入帐号密码
driver.find_element_by_xpath("//*[@id='email-login-input']/div/div[2]/input").send_keys("user")
driver.find_element_by_xpath("/html/body/div/div/div[2]/div[2]/div[2]/div[4]/div/div/div[2]/input").send_keys("password")
#单次触摸 登录按钮
Action = TouchActions(driver)
loginButton = driver.find_element_by_xpath("/html/body/div/div/div[2]/div[2]/div[2]/button")
Action.tap(loginButton)
Action.perform()
#触摸后,有触摸的反馈,但仍无法跳转到 登录成功页
  1. 使用简单粗暴的方法send_keys(Keys.ENTER),模拟点击 回车键,可正常登录
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from selenium.webdriver.common.keys import Keys

mobile_emulation = {"deviceName": "iPhone X"}
options = Options()
options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://xxx.com")
#输入帐号密码
driver.find_element_by_xpath("//*[@id='email-login-input']/div/div[2]/input").send_keys("user")
driver.find_element_by_xpath("/html/body/div/div/div[2]/div[2]/div[2]/div[4]/div/div/div[2]/input").send_keys("password")
#定位“登录按钮”,按“回车键”
loginButton = driver.find_element_by_xpath("/html/body/div/div/div[2]/div[2]/div[2]/button")
loginButton.send_keys(Keys.ENTER)
#点击 “回车键” 后,可正常跳转到 登录成功页,问题解决

webdriver 的Keys()类提供键盘上所有按键的操作

from selenium.webdriver.common.keys import Keys
#在使用键盘按键方法前需要先导入keys 类包。
#下面经常使用到的键盘操作:
send_keys(Keys.BACK_SPACE) #删除键(BackSpace)
send_keys(Keys.SPACE) #空格键(Space)
send_keys(Keys.TAB) #制表键(Tab)
send_keys(Keys.ESCAPE) #回退键(Esc)
send_keys(Keys.ENTER) #回车键(Enter)
send_keys(Keys.CONTROL,'a') #全选(Ctrl+A)
send_keys(Keys.CONTROL,'c') #复制(Ctrl+C)

猜你喜欢

转载自blog.csdn.net/sonyv/article/details/82979409
今日推荐