WebDriver 跳转新增页面和返回原页面-点击某一按钮新增click,并切换页面

应用场景:
对于web端接收手机验证码的处理方法:
1、如果有权限可以通过查询数据库来获得手机验证码,方便快捷。
2、如果后台系统保存了手机验证码,可以去后台获取验证码,然后填写到前台页面,此方法有两种处理方法
2.1 在前台发送验证码的页面,直接通过dr.get("http://www.xxx.com");切换页面到后台,查询验证码并保存下来,再通过
dr.get("http://www.xxx.com");方法切换到前台发送验证码页面,充填之前填写信息,此时不点击发送手机验证码,直接输入上次发送的手机验证码

2.2 可通过js以及切换页面来实现,具体操作如下:
通过js给发送验证码的按钮新增onclick()事件,onclick()事件可通过window.open("http://www.baidu.com");方法给按钮新增重新打开一页面方法,通过该方法在当前页面后面新打开一个页面(此时是打开后台地址)
在第二个页面获得验证码,此时关闭第二个页面,返回第一个页面输入验证码,完成操作
主要对2.2 的方法进行详细说明
对发送验证码按钮新增onclick事件:
    JavascriptExecutor js = (JavascriptExecutor)dr;
WebElement e = driver.findElement(By.xpath(".//*[@id='lg']/img"));
js.executeScript("arguments[0].οnclick=function(){window.open('http://t0.gomemyc.com:8080/admin/message/listAll')}",e);


切换到第二个页面方法



//页面跳转到第二第三窗口
public void getHandle(WebDriver driver) {

try {
Thread.sleep(1500);
String currentWindow = driver.getWindowHandle(); //获取当前窗口句柄
System.out.println("当前:"+currentWindow);
Thread.sleep(3000);
Set<String> handles = driver.getWindowHandles(); //获取最新窗口句柄
Iterator<String> it = handles.iterator();

while(it.hasNext()){
String next = it.next();
if(currentWindow.equals(next))
{
System.out.println("第一次跳转:"+next);
continue;
}

WebDriver window = driver.switchTo().window(next);
// aa = true;
System.out.println("title,url = "+window.getTitle()+","+window.getCurrentUrl());

}
System.out.println("跳转结束");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (NoSuchElementException ee) {
// TODO Auto-generated catch block
ee.printStackTrace();
}catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}


}

切换到第二个页面后,再获得验证码信息,此时再关闭第二个页面,跳转到第一个页面

//此方法是将页面跳转到第一个窗口
public void getHandle2(WebDriver driver) {

try {
Thread.sleep(4000);
String currentWindow = driver.getWindowHandle(); //得到当前窗口句柄
System.out.println(driver.getCurrentUrl());
System.out.println("当前:"+currentWindow);
Thread.sleep(1900);
Set<String> handles = driver.getWindowHandles(); //得到所有窗口句柄
Iterator<String> it = handles.iterator();
String next = it.next(); //此处是第一个窗口句柄
System.out.println("第一个窗口句柄:"+next);
WebDriver window = driver.switchTo().window(it.next()); //跳转第二个窗口
Thread.sleep(1900);
window.close(); //关闭第二个窗口
Thread.sleep(1900);
window = driver.switchTo().window(next); //此处跳转到第一个窗口
System.out.println("title,url = "+window.getTitle()+","+window.getCurrentUrl());

System.out.println("跳转结束");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (NoSuchElementException ee) {
// TODO Auto-generated catch block
ee.printStackTrace();
}catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}


}




转载于:https://www.cnblogs.com/wxjtest/p/6288995.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值