【WebDriver】画面操作不稳定性的解决方案


■常见现象1: 控件操作无效,但是没有任何报错信息

如果经常使用selenium WebDriver进行自动化测试,会发现有个现象很常见,控件操作的代码已经走完,并且没有任何错误,但是操作并没有生效。

例如,画面上有四个checkbox,需要打上勾:

WebElement forFixed = d.findElement(By.xpath("//input[@name = 'ForFixed']"));
WebElement forNoFixed = d.findElement(By.xpath("//input[@name = 'ForNoFixed']"));
WebElement forUnconfirm = d.findElement(By.xpath("//input[@name = 'ForUnconfirm']"));
WebElement forDefferd = d.findElement(By.xpath("//input[@name = 'ForDefferd']"));
if(forFixed.isSelected() == false){
	forFixed.click();
}
if(forNoFixed.isSelected() == false){
	forNoFixed.click();
}
if(forUnconfirm.isSelected() == false){
	forUnconfirm.click();
}
if(forDefferd.isSelected() == false){
	forDefferd.click();
}

代码运行没有任何出错信息,但是跑完之后发现四个控件中,总有一个没有被check上。貌似这种现象很常见,这也算selenium WebDriver的一个缺陷吧。

解决方法:

追加一个循环操作,直到所有的按钮都被选中之后再退出循环继续下一步的操作。

//Wait for select all checkbox was checked.
(new WebDriverWait(arg0, 20)).until(new ExpectedCondition<Boolean>() {
	public Boolean apply(WebDriver d) {
		
		WebElement forFixed = d.findElement(By.xpath("//input[@name = 'ForFixed']"));
    		WebElement forNoFixed = d.findElement(By.xpath("//input[@name = 'ForNoFixed']"));
    		WebElement forUnconfirm = d.findElement(By.xpath("//input[@name = 'ForUnconfirm']"));
    		WebElement forDefferd = d.findElement(By.xpath("//input[@name = 'ForDefferd']"));
		if(forFixed.isSelected() == false){
    			forFixed.click();
    		}
    		if(forNoFixed.isSelected() == false){
    			forNoFixed.click();
    		}
    		if(forUnconfirm.isSelected() == false){
    			forUnconfirm.click();
    		}
    		if(forDefferd.isSelected() == false){
    			forDefferd.click();
    		}
		if((forFixed.isSelected() == true)&&(forNoFixed.isSelected() == true)&&(forUnconfirm.isSelected() == true)&&(forDefferd.isSelected() == true)){
			return true;
		}else{
			return false;
		}
	}
});



■常见现象2: 当点击Submit型Button时,form中的参数会不明原因的丢失,导致后续的画面打开时发生错误。

WebElement elementSmt = driver.findElement(By.name("Do_Corrective"));
elementSmt.submit();

在网上搜索了很久,终于找到了一种比较靠谱的方法,使用js代码替代selenium的sumit函数!!

IE解决方法:   

((JavascriptExecutor) arg0).executeScript("document.getElementsByName('Do_Corrective')[0].click()");

Firefox解决方法:  

driver.navigate().to("javascript:document.getElementsByName('Do_Corrective')[0].click()");

-----------------------------------------

(遇到新的问题会继续补充,上面有错的地方,请联系我!(*^__^*) 嘻嘻……)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值