【慎用 findElement】
今天本打算研究一下 异步加载的延迟测试,结果一不留神发现了 findElement 的问题。
不敢说这是个Bug,不排除是自己学艺不精,暂且把问题抛出来,希望大家能给些意见。
1、问题描述:
使用 FireFoxDriver ,用 findElement 去查找页面上不存在的元素时,会导致程序死在 findElement 上,停滞不前,不会继续执行后面的代码
即使在 WebDriverWait 的 until 内使用也会导致锁死,而且 until 内的代码只会执行一次, timeout 完全失效
此问题在 IE、Chrome 上不会出现,会正常报错
换成 findElements 后可以正常工作
换成 用 js 方法获取对象 可以正常工作
2、问题跟踪
经过对源码的跟踪,发现是在 org.openqa.selenium.remote.HttpCommandExecutor 的 private HttpResponse fallBackExecute 方法内 执行 return client.execute(targetHost, httpMethod, context); 后就停滞了...
无任何异常抛出
3、测试代码
package lesson3;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ExampleForAjax {
static WebDriver driver;
@BeforeClass
public static void init() {
System.out.println("init...");
// 如果你的 FireFox 没有安装在默认目录,那么必须在程序中设置
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
// 创建一个 FireFox 的浏览器实例
driver = new FirefoxDriver();
}
@Test
public void test() {
// 让浏览器访问 zTree Demo
driver.get("http://www.ztree.me/v3/demo/cn/core/standardData.html");
// 等待 zTree 初始化完毕,Timeout 设置10秒
try {
(new WebDriverWait(driver, 5, 500)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
//findElement 会导致程序锁死
// WebElement element = driver.findElement(By.id("treeDemo111"));
// return element != null;
WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#treeDemo111');");
return element != null;
}
});
} catch(Exception e) {
e.printStackTrace();
}
try {
//findElement 会导致程序锁死
// WebElement element = driver.findElement(By.id("treeDemo111"));
List<WebElement> elementList =(List<WebElement>) driver.findElements(By.id("treeDemo111"));
System.out.println(elementList.size());
} catch(Exception e) {
e.printStackTrace();
}
try {
//findElement 会导致程序锁死
// WebElement element = driver.findElement(By.id("treeDemo111"));
WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#treeDemo111');");
} catch(Exception e) {
e.printStackTrace();
}
}
@AfterClass
public static void destory() {
System.out.println("destory...");
//关闭浏览器
driver.quit();
}
}
总之,目前感觉尽量慎用 findElement 这个方法;利用 findElements 或 js 方法来替换,以避免异常出现。
希望有这方面经验的朋友多交流一下。
分享到:
相关推荐
WebDriver 的常用API使用方法,第3部分自动化测试框架搭建篇深入讲解了页面对象的设计模式以及分布式并发执行测试框架、数据驱动测试框架、行为驱动测试框架和关键字驱动测试框架的实例源码。第4部分为常见问题和姐...
Selenium WebDriver 3 Practical Guide will walk you through the various APIs of Selenium WebDriver, which are used in automation tests, followed by a discussion of the various WebDriver implementations...
selenium RC与selenium webdriver的区别
selenium webdriver是web自动化的一本经典著作,吴老集合python java不同版本。本资源为java版本,内容较新,无论是入门还是提升都有很大帮助。
关于Selenium WebDriver工作原理的介绍,原理相关描述
Selenium WebDriver 3.14.0 本来需要.net45 我改为.net2.0 希望喜欢的人需要。 dll 反编译,嵌入程序内。无需调用那个讨厌的dll。 绝对不骗人,骗人是小狗。架构复杂修改麻烦,各位体谅
amazon 3星+ 好评 Key Features Covers basic to advanced concepts of WebDriver Learn how to design a more effective automation framework Explores all of the APIs within WebDriver Acquire an in-...
seleniumwebdriver(python) (第三版)
selenium webdriver基于python源码案例,全是案例适合小白入门学习
selenium2.0自动化
Selenium WebDriver 学习笔记 API大全 中文API 元素 操作大全
selenium WebDriver 安装包及启动服务的一些命令 对于正在学习自动化测试的小伙伴们非常有用
1.2 selenium webdriver学习(二)————对浏览器的简单操作 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.3 selenium webdriver学习(三)------------执行js脚本 . . . . . . . . . . . . . ...
Selenium WebDriver Recipes in C#(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
对于Selenium更加详细的操作和使用,推荐一本书《selenium webdriver(python)第三版》,该书详细的讲解了webdriver的用法
本教程是来源与书 Selenium Testing Tools cookbook,但是其中的所有代码都是通过python来实现并且调试通过的。此文档为版本为粗略版,后期会不断的更新优化
自动化测试:Selenium webdriver学习笔记C#版
Selenium Webdriver自动化测试 operadriver win64位,最新版本(20150611)
悠悠的selenium webdriver基于python源码案例,全是案例适合小白入门学习,现在有的资源都是只有4章
selenium webdriver2 环境搭建