我遇到的问题
- 获取到 登录按钮 的 xpath,且可以保证 xpath 正确无误
- 点击 登录按钮, 无法正常跳转到 登录成功页
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()
- 触摸 登录按钮, 无法正常跳转到 登录成功页
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()
- 使用简单粗暴的方法
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
send_keys(Keys.BACK_SPACE)
send_keys(Keys.SPACE)
send_keys(Keys.TAB)
send_keys(Keys.ESCAPE)
send_keys(Keys.ENTER)
send_keys(Keys.CONTROL,'a')
send_keys(Keys.CONTROL,'c')