美文网首页
selenium 延迟等待的三种方式

selenium 延迟等待的三种方式

作者: 宇文臭臭 | 来源:发表于2017-03-13 13:57 被阅读0次

1、最直接普通的方式:这个是设置固定的等待时间

Thread.sleep(1000);

2、隐式等待方式(implicitlyWait):设置脚本在查找元素时的最大等待时间;

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

代码示例如下:

public boolean isByElementDisplayed(By by, int time,WebDriver chrome) {

boolean status = true;

while(!isByPresent(chrome, by)){

chrome.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

}

return status;

}

public boolean isByPresent(WebDriver chrome, By by){

boolean display = false;

try{

chrome.findElement(by).isDisplayed();

return display= true;

}catch(NoSuchElementException e){

return display;

}

}

这里用while循环是如果明确需要查找的元素未找到,就继续循环等待;

3、显示等待方式(Explicit Wait):就是明确的要等待的元素在规定的时间之内都没找到,那么就抛出Exception.代码示例如下:

new WebDriverWait(chrome, 15).until(

ExpectedConditions.presenceOfElementLocated(By.cssSelector("css locator"))

);

这里,15是要等待的秒数.如果没有满足until()方法中的条件,就会始终在这里wait 15秒,依然找不到,就抛出异常 。

还可以这样写:

WebDriver chrome= new ChromeDriver() ;

chrome.get( http://somedomain/url_that_delays_loading);

WebElement e = (new WebDriverWait( chrome, 10)) .until(

new ExpectedCondition< WebElement>(){

@Override  //方法重写

public WebElement ByPresent( WebDriver d) {

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

}

}

);

这样就通过回调函数,直接获得了这个WebElement.也就是页面元素 。

相关文章

  • python + Selenium合集

    python + Selenium 合集 selenium原理,点这里selenium 三种等待方式,点这里~ 五...

  • selenium 延迟等待的三种方式

    1、最直接普通的方式:这个是设置固定的等待时间 Thread.sleep(1000); 2、隐式等待方式(impl...

  • selenium三种等待方式

    selenium三种等待方式[https://www.cnblogs.com/ctltest/p/14480682...

  • selenium 三种等待方式

    selenium 的三种等待方式 上面的隐式等待包括:WebDriverWait 默认每 500 毫秒调用一次 E...

  • selenium三种等待方式

    随着对selenium不断的深入学习,在网上搜索的资料越多,才发现自己掌握的只是冰山一角。 1、强制等待 用法:t...

  • selenium三种等待方式

    作者:Gakki 前言 在浏览器加载一个页面时,页面内得元素可能是在不同的时间载入的,这会加大定位元素的困难程度,...

  • Selenium的三种等待方式

    直接等待 time.sleep(1),强制等待线程休眠一定时间 使用上简单粗暴 以灵题库网站账号密码登录(http...

  • Selenium 三种等待元素出现的方式

    Selenium 等待元素出现的方式有以下三种1、显式等待通俗点说,就是死等,很死板不灵活的等待。即在指定的时间内...

  • Selenium三种等待方式的使用

    UI自动化测试,大多都是通过定位页面元素来模拟实际的生产场景操作。但在编写自动化测试脚本中,经常出现元素定位不到的...

  • 关于Selenium里等待的理解

    关于Selenium里等待的理解 首先使用selenium做自动化测试时有时需要等待元素的加载完成,常用的等待方式...

网友评论

      本文标题:selenium 延迟等待的三种方式

      本文链接:https://www.haomeiwen.com/subject/oswfnttx.html