您的位置:首页 > Web前端 > JavaScript

Selenium WebDriver等待页面元素加载完成

2013-08-21 11:27 686 查看
<html>

 <head>

 <title>Set Timeout</title>

  <style>

  .red_box {background-color: red; width = 20%; height: 100px; border: none;}

  </style>

  <script>

   function show_div(){

    d = document.createElement('div');

    d.className = "red_box";

    document.body.appendChild(d);

   }

  </script>

 </head>

 <body>

  <button id = "b1" onclick = "show_div()">click1</button>

  <button id = "b2" onclick = "show_div()">click2</button>

 </body>

</html>

 

@Test

 public void TestRun() throws Exception{

  String url="D:\\Selenium\\JavaS\\JavaTest\\html\\waitforcondition\\Wait.html";
  //方法一:设置10秒

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

  dr.get(url);

  dr.findElement(By.id("b1")).click();

  WebElement element1 = dr.findElement(By.cssSelector(".red_box"));

  ((JavascriptExecutor)dr).executeScript("arguments[0].style.border = \"5px solid yellow\"",element1);  

  
  //方法二:重写了ExpectedCondition接口中的apply方法
  String url1="D:\\Selenium\\JavaS\\JavaTest\\html\\waitforcondition\\Wait.html";

  dr.get(url1); 

  WebDriverWait wait = new WebDriverWait(dr,10);

  wait.until(new ExpectedCondition<WebElement>(){

  @Override

  public WebElement apply(WebDriver d) {

  return d.findElement(By.id("b2"));

  }}).click();

  WebElement element2 = dr.findElement(By.cssSelector(".red_box"));

  ((JavascriptExecutor)dr).executeScript("arguments[0].style.border = \"5px solid yellow\"",element2);    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息