滑块作为安全验证机制的一种,经常在登录或者注册时涉及。但是在自动化测试时,需要想办法用代码的方式来处理滑块。
selenium中对滑块的操作基本是采用元素拖曳的方式,而这种方式需要用到selenium的Actionchains功能模块的drag_and_drop_by_offset方法。
示例:以携程网的注册页面为例,URL:https://passport.ctrip.com/user/reg/home/
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
d = webdriver.Chrome()
d.maximize_window()
d.implicitly_wait(10)
d.get('https://passport.ctrip.com/user/reg/home')
d.find_element(By.XPATH, '//div[@class="pop_footer"]/a[@class="reg_btn reg_agree"]').click()
ele_button = d.find_element(By.XPATH, '//div[@class="cpt-drop-btn"]')
ele = d.find_element(By.XPATH, '//div[@class="cpt-bg-bar"]')
ActionChains(d).drag_and_drop_by_offset(ele_button, ele.size['width'], ele.size['height']).perform()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)