java selenium智能等待页面加载完成示例代码

所属分类: 网络编程 / ASP.NET 阅读数: 1608
收藏 0 赞 0 分享

java selenium  智能等待页面加载完成

我们经常会碰到用selenium操作页面上某个元素的时候, 需要等待页面加载完成后, 才能操作。  否则页面上的元素不存在,会抛出异常。 

或者碰到AJAX异步加载,我们需要等待元素加载完成后, 才能操作

selenium 中提供了非常简单,智能的方法,来判断元素是否存在. 

阅读目录

  1. 实例要求
  2. 隐式等待
  3. 显式等待

实例要求

实例:set_timeout.html 下面的html 代码,  点击click 按钮5秒后, 页面上会出现一个红色的div快, 我们需要写一段自动化脚本智能的去判断这个div是否存在, 然后把这个div 然后高亮。

<html>
 <head>
  <title>Set Timeout</title>
  <style>
   .red_box {background-color: red; width = 20%; height: 100px; border: none;}
  </style>
  <script>
   function show_div(){
    setTimeout("create_div()", 5000);
   }
 
   function create_div(){
    d = document.createElement('div');
    d.className = "red_box";
    document.body.appendChild(d);
   }
  </script>
 </head>
 <body>
  <button id = "b" onclick = "show_div()">click</button>
 </body>
</html>

隐式等待

  WebDriver driver = new FirefoxDriver();
  driver.get("file:///C:/Users/Tank/Desktop/set_timeout.html"); 
  
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  WebElement element = driver.findElement(By.cssSelector(".red_box"));  
  ((JavascriptExecutor)driver).executeScript("arguments[0].style.border = \"5px solid yellow\"",element); 

其中

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

意思是, 总共等待10秒, 如果10秒后,元素还不存在,就会抛出异常  org.openqa.selenium.NoSuchElementException

显式等待

显式等待 使用ExpectedConditions类中自带方法, 可以进行显试等待的判断。

显式等待可以自定义等待的条件,用于更加复杂的页面等待条件

等待的条件

WebDriver方法

页面元素是否在页面上可用和可被单击

elementToBeClickable(By locator)

页面元素处于被选中状态

elementToBeSelected(WebElement element)

页面元素在页面中存在

presenceOfElementLocated(By locator)

在页面元素中是否包含特定的文本

textToBePresentInElement(By locator)

页面元素值

textToBePresentInElementValue(By locator, java.lang.String text)

标题 (title)

titleContains(java.lang.String title)

只有满足显式等待的条件满足,测试代码才会继续向后执行后续的测试逻辑

如果超过设定的最大显式等待时间阈值, 这测试程序会抛出异常。

public static void testWait2(WebDriver driver)
 {
  driver.get("E:\\StashFolder\\huoli_28@hotmail.com\\Stash\\Tank-MoneyProject\\浦东软件园培训中心\\我的教材\\Selenium Webdriver\\set_timeout.html"); 
  
  WebDriverWait wait = new WebDriverWait(driver, 20);
  wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".red_box")));
  WebElement element = driver.findElement(By.cssSelector(".red_box"));  
  ((JavascriptExecutor)driver).executeScript("arguments[0].style.border = \"5px solid yellow\"",element); 
 }

以上就是对Java selenium 等待页面加载的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!

更多精彩内容其他人还在看

ASP.NET 水晶报表打印功能实现代码

ASP.NET下的水晶报表打印,据我所知有以下几种办法可以打印
收藏 0 赞 0 分享

ASP.Net 图片存入数据库的实现代码

在很多时候,我们有这样的需求:把图片存入到数据库当中。在一些应用程序中,我们可能有一些敏感的资料,由于存储在文件系统(file system)中的东西,将很容易被某些用户盗取,所以这些数据不能存放在文件系统中。
收藏 0 赞 0 分享

让Silverlight 2.0动画动起来Making Silverlight 2.0 animation Start(不能运动原因)

Microsoft Expression Blend 2 制作动画个人感觉倒像3DMAX 可以自动捕捉关键帧
收藏 0 赞 0 分享

asp.net Reporting Service在Web Application中的应用

由于我们这个项目中使用微软的报表服务(Reporting Services)作为报表输出工具,本人也对它进行一点点研究,虽没有入木三分,但这点知识至少可以在大部分Reporting Service的场景中应用。
收藏 0 赞 0 分享

C# 文件上传 默认最大为4M的解决方法

.net中默只能上传小于4m的文件,大于4M将无法显示页面.那么如何设置来使imputfile能上传更大的文件呢
收藏 0 赞 0 分享

asp.net 购物车实现详细代码

asp.net 购物车实现详细代码
收藏 0 赞 0 分享

asp.net repeater实现批量删除时注册多选框id到客户端

repeater批量删除时注册多选框id到客户端的实现代码
收藏 0 赞 0 分享

asp.net aspnetpager分页统计时与实际不符的解决办法

最近分页方面根据实际需要修改了一些函数
收藏 0 赞 0 分享

iis 服务器应用程序不可用的解决方法

访问页面时提示 服务器应用程序不可用,大家可以按照下面的方法重新注册下,应该能好点
收藏 0 赞 0 分享

asp.net button 绑定多个参数

asp.net button 绑定多个参数的代码
收藏 0 赞 0 分享
查看更多