有如下登录页面:
登录过程中输入账号密码后 需要拉动图中 滑块 后 才可登录成功:
如何使用webdriver 实现拉动滑块的功能?代码如下:
创建公共功能类并加入方法:
public class ActionUtil { private static Actions act; public static void setAction(WebDriver driver){ act = new Actions(driver); } public static void DRAG_ELEMENT(WebElement e,int xOffset,int yOffset){ Action dragAllTheWayUp = act.moveToElement(e).clickAndHold().moveByOffset(xOffset, yOffset).release().build(); dragAllTheWayUp.perform(); } }在登录页面的pageobject类中加封装如下方法:
注入滑块页面对象
@FindBy(how = How.ID, using = "nc_1_n1z") private WebElement Sliders;
public void dragSliders() { if (Sliders.isEnabled()) { ActionUtil.DRAG_ELEMENT(Sliders, 258, 0); } }
public void login(String name, String pwd) { loginName.sendKeys(name); password.sendKeys(pwd); dragSliders(); loginButton.submit(); }