web自动化之小白入门--ActionChains模拟鼠标操作 解决密码框无法输入问题

489 阅读3分钟

“登录百度账号”为例进行web自动化测试:

百度页面的登录入口,由【登录】超链接点击弹出div窗口,需输入“手机号/用户名/邮箱”、“密码”进行登录。简单梳理下逻辑,如下,看着似乎很简单的样子哈。。。。。

image.png

image.png

逻辑步骤:

1、先在页面定位到“登录”超链接,点击打开输入弹窗。
2、继续定位到“手机号/用户名/邮箱”元素输入框。
3、定位到“密码”输入框。
4、点击“登录”按钮

然而,实际情况下也是一顿代码操作猛如虎,各种元素定位,代码写完运行,结果发现密码框一直没有自动输入代码中的密码,无法进行下一步

想解决办法ing......

为可正常输入密码且能登录成功,作为一个python小白也是不眠不休,到了午觉时间,瞬间也精神抖擞啊,各种网上查资料元素定位不到情况,终于找到思路。

解决问题思路:

a、先查看元素是否定位成功
定位密码元素后,通过get_attribute("class")方法获取class属性并赋值给变量,再将变量打印出来,

class_value = driver.find_element(By.XPATH, "//input[@name='password' and @autocomplete='off']").get_attribute("class")
print(class_value + "\n" + "杨小二test")

可正常打印密码输入框class属性,但属性为默认值,鼠标未点击定位到输入框,密码框依旧为空,需继续涨知识ing.....

image.png

b、需模拟鼠标移动、点击按键操作:

通过F12反复鼠标定位发现,“密码”框的class属性会随着鼠标动作不同而变动,存在如下4种场景:\

class='pass-text-input pass-text-input-password' -----上图为【鼠标远离密码框】场景;
class='pass-text-input pass-text-input-password `pass-text-input-hover`' -----为鼠标经过密码框,【悬浮未点击】场景;
class='pass-text-input pass-text-input-password `pass-text-input-hover pass-text-input-focus`'    ----为【鼠标点击且悬浮】场景
class='pass-text-input pass-text-input-password `pass-text-input-focus`'    ----为【鼠标点击然不悬浮】场景

(1) 查到需使用ActionChains 模拟鼠标操作

# ActionChains模拟鼠标移动、按键操作
my_action = ActionChains(driver)

(2)导入ActionChains包

from selenium.webdriver import ActionChains

(3) 使用鼠标对象my_action调用move_to_element().perform(),其中move_to_element()参数为浏览器驱动对象的定位元素操作,具体如下代码:

# 定位成功,将鼠标放在该元素上
my_action.move_to_element(driver.find_element(By.XPATH, "//input[@name='password' and @autocomplete='off']")).perform()

(4) 最后以为可以在上面的perform()后直接调用.send_keys()方法一步到位no no no,臣妾做不到啊......

my_action.move_to_element(driver.find_element(By.XPATH, "//input[@name='password' and @autocomplete='off']")).perform().send_keys("xxxxxx")

结果不行,提示对象无此函数,so,又需浏览器驱动重新调用元素定位并输入元素

# 重新定位元素并点击元素,再输入【密码】
driver.find_element(By.XPATH, "//input[@name='password' and @autocomplete='off']").send_keys("xxxxxx")

到此上文提到的问题终于得到解决,over啦,心情很舒畅倒是

如下是我编写的python简单线性代码,供追溯:

from time import sleep

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
# ActionChains模拟鼠标移动、按键操作
my_action = ActionChains(driver)

driver.implicitly_wait(5)
driver.get("https://www.baidu.com/?tn=baiduhome_pg")
driver.find_element(By.LINK_TEXT, "登录").click()

# 定位【手机号/用户名/邮箱】
driver.find_element(By.NAME, "userName").send_keys("xxxxxx")
# 查看【密码】元素是否定位成功,通过打印元素class属性查看
class_value = driver.find_element(By.XPATH, "//input[@name='password' and @autocomplete='off']").get_attribute("class")
print(class_value + "\n" + "杨小二test")
# 定位【密码】成功,将鼠标放在该元素上
my_action.move_to_element(driver.find_element(By.XPATH, "//input[@name='password' and @autocomplete='off']")).perform()
# 重新定位元素并点击元素,再输入【密码】
# driver.find_element(By.XPATH, "//input[@name='password' and @autocomplete='off']").click()
driver.find_element(By.XPATH, "//input[@name='password' and @autocomplete='off']").send_keys("xxxxxxxx")
# 定位【登录】按钮
driver.find_element(By.XPATH, "//input[@id ='TANGRAM__PSP_11__submit' and @value='登录']").click()
driver.maximize_window()
sleep(3)
driver.quit()

web自动化引入unittest单元测试框架:
1、新建一个类,继承unittest.TestCase
2、导入unittest包
3、新建一个方法,以test_开头的测试用例\