selenium Explicit 和 Implicit Waits区别

本文详细介绍了Selenium中显式等待(Explicit Waits)和隐式等待(Implicit Waits)的区别和用法。显式等待是根据预期条件明确等待,而隐式等待是在尝试查找元素时全局设定的超时时间。警告指出不要同时使用两者,以免造成不稳定的等待时间。文章还提供了一些常见的预期条件示例,并讨论了在不同场景下选择哪种等待方式更为合适。

The Selenium Browser Automation Project | Selenium

Explicit and Implicit Waits

Waiting is having the automated task execution elapse a certain amount of time before continuing with the next step. You should choose to use Explicit Waits or Implicit Waits.

WARNING: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.

Explicit Waits 显式等待

An explicit /ɪkˈsplɪsɪt明确的;清楚的;/  wait is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

WebDriver driver = new FirefoxDriver(); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));

This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return value for the ExpectedCondition function type is a Boolean value of true, or a non-null object.

This example is also functionally equivalent to the first Implicit Waits example.

Expected Conditions

There are some common conditions that are frequently encountered when automating web browsers. Listed below are a few examples for the usage of such conditions. The Java and Python bindings include convenience methods so you don’t have to code an ExpectedCondition class yourself or create your own utility package for them.

  • Element is Clickable - it is Displayed and Enabled.

WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

 in

The ExpectedConditions package (Java) (Python) (.NET) contains a set of predefined conditions to use with WebDriverWait.

显式等待,就是明确的要等到某个元素的出现或者是某个元素的可点击等条件,等不到,就一直等,除非在规定的时间之内都没找到,那么就跳出Exception.

如:

new WebDriverWait(driver, 15).until(

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

);

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

也可以这样写:

WebDriver driver = new FirefoxDriver();

driver.get( www.baidu.com);

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

new ExpectedCondition< WebElement>(){

@Override

public WebElement apply( WebDriver d) {

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

}

}

);

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

如果只是仅仅想判断页面是不是加载到某个地方了,就可以用第一种方法; 但如果需要得到某个WebElement,两种方式都可以,只是第一种方式还需要再多一步获取的操作.

Wait w = new Wait(){

@Override

public boolean until() {

return webElement.isDisplayed();

}

};

另外这种等待的方式,在加载js代码的时候做判断会比较方便,反正我只在代码里这么见过,但是没用过,有兴趣的朋友可以研究一下.

Implicit Waits 隐式等待

An implicit /ɪmˈplɪsɪt隐式;含蓄的/  wait is to tell WebDriver to poll /pəʊl 投票;轮询/ the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

https://blog.csdn.net/pf20050904/article/details/20052485#commentBox

最近在项目过程中使用selenium 判断元素是否存在的时候 遇到一个很坑爹的问题, 用以下方法执行的时候每次都会等待很长一段时间,原因是因为对selenium实现方法了解不足导致一直找不到解决方法。

private boolean isElementPresent(By by) {

try {

driver.findElement(by);

return true;

} catch (NoSuchElementException e) {

return false;

}

}

不明所以, 经过追踪源码才发现启动Driver的时候 使用driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 此处设置的等待时间 是针对全局设置的,webdriver中执行所有命令的超时时间都设置为30秒了, 如上面的findElement方法,找不到元素会默认等待三十秒。  有些时候只需简单判断元素是否存在,立马执行,而此处的设置导致脚本执行缓慢。

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

隐式等待, 此处的隐式等待是针对Driver 每次执行命令的最长执行时间也可以理解为超时时间,一些人对此处有误解,认为是让Driver等一段时间,  确实某些时候能让Driver等一段时间, 但是影响是全局的,每次Driver执行找不到元素都会等待此处设置的时间,假设某处将此值设置的太长,必须在执行完成之后还原回来,否则判断一个元素是否存在的时候,就会遇到很坑爹的问题。   改进后的方法如下:

WebDriver会进行一个隐式等待,但参数只有时间,这就导致我需要什么元素出现,我不一定能等到它,

private boolean isElementPresent(By by) {

try {

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

driver.findElement(by);

return true;

} catch (NoSuchElementException e) {

return false;

}

}

得不到某个元素,我们就延迟一下...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值