Selenium impilicit、explicit和fluentwait之间的差异

Selenium impilicit、explicit和fluentwait之间的差异,selenium,synchronization,Selenium,Synchronization,implicitwait()、explicitwait()和fluentwait()之间的确切区别是什么?您能举例说明吗?显式等待是您定义的等待特定条件发生后再继续执行代码的代码。最糟糕的情况是Thread.sleep(),它将条件设置为等待的确切时间段。提供了一些方便的方法,可以帮助您编写只在需要时等待的代码WebDriverWait结合ExpectedCondition是实现这一点的一种方法。示例如下: WebDriverWait wait = new WebDriverWait(drive

implicitwait()
explicitwait()
fluentwait()
之间的确切区别是什么?您能举例说明吗?

显式等待是您定义的等待特定条件发生后再继续执行代码的代码。最糟糕的情况是
Thread.sleep()
,它将条件设置为等待的确切时间段。提供了一些方便的方法,可以帮助您编写只在需要时等待的代码
WebDriverWait
结合
ExpectedCondition
是实现这一点的一种方法。示例如下:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
但是,根据语言实现的不同,会有一些不同。有关
ExpectedCondition
接口的更多信息

隐式等待是告诉
WebDriver
在试图查找一个或多个元素(如果它们不立即可用)时轮询
DOM
一段时间。默认设置为0。一旦设置,隐式等待将被设置为
WebDriver
对象实例的生命周期。下面是隐式等待的实现:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
这两个定义都来自于最完美的定义


流畅的等待只是另一种等待元素的机制。它提供了一些独特的机制来轮询
DOM
以查找元素。它提供的最大功能之一是等待元素忽略某些
异常。请参阅隐式等待:

隐式等待是告诉WebDriver在尝试查找元素时轮询DOM一段时间(如果元素不立即可用)。WebDriver将等待所述时间,并且在指定的时间段内不会再次尝试查找该元素。一旦指定的时间结束,它将在引发异常之前的最后一次尝试再次搜索元素。主要缺点是时间太长,因为驱动程序在继续之前等待指定的时间

显式等待

可能有一种情况,当一个特定的元素需要超过一分钟的时间来加载。在这种情况下,您可以只在所需的元素上放置一个单独的时间。按照此操作,浏览器隐式等待时间对于每个元素都会很短,而对于特定元素则会很长。默认轮询周期为500毫秒(即)它将每隔500毫秒检查一次元素

流畅等待 如果您有一个元素,它有时仅在1秒内出现,有时需要几分钟才能出现。在这种情况下,最好使用fluent wait,因为这将一次又一次地尝试查找元素,直到找到它或直到最后一个计时器用完。您可以在fluent wait中设置轮询时间 用户还可以将等待配置为忽略特定类型的异常,例如NoTouchElementException

 // Waiting 30 seconds for an element to be present on the page, checking
   // for its presence once every 5 seconds.
   Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(NoSuchElementException.class);

   WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
     public WebElement apply(WebDriver driver) {
       return driver.findElement(By.id("foo"));
     }
   })
//等待30秒,让元素出现在页面上,检查
//每5秒检查一次。
等待等待=新建FluentWait(驱动程序)
.带超时(30秒)
.每(5秒)轮询一次
.忽略(NoSuchElementException.class);
WebElement foo=wait.until(新函数(){
公共WebElement应用(WebDriver){
返回驱动程序findElement(By.id(“foo”);
}
})
我发了帖子,我想我提供了一些其他答案遗漏的细节

隐式等待:在隐式等待期间,如果Web驱动程序由于其可用性而无法立即找到它,则Web驱动程序将定期轮询DOM(间隔0.5秒或取决于驱动程序浏览器实现),直到达到默认的隐式最大等待时间。一旦指定的隐式wait max time结束,它将在引发WebDriverException(如NoTouchElementException)之前的最后一次尝试再次搜索元素。默认设置为0,这意味着对driver.findElement的调用将不需要轮询DOM,如果元素确实存在,它将在0–999毫秒内立即返回,或者如果在同一时间段内不存在,它将抛出NoTouchElementException。要覆盖默认的最大时间,请执行以下操作:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
显式等待:当某个特定元素的加载时间超过一秒或更长时,可能会出现实例。在这种情况下,您肯定不想设置一个巨大的隐式等待时间,因为如果您这样做,那么您的浏览器将为每个驱动程序调用等待相同的最长时间来查找元素。因此,您可能会注意到测试性能的轻微下降

为了避免这种情况,您只需在所需元素上定义一个单独的等待时间。通过遵循此规则,浏览器隐式等待时间对于每个驱动程序调用查找元素来说都很短,对于一个特定的元素,它可能会很长

显式等待总是首先定义FluentWait,例如WebDriverWait对象,然后通常使用预期的条件来解决等待

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(“aId”)));
Fluent Wait:假设您有一个元素,它有时仅在1秒内出现,有时需要几分钟才能出现。在这种情况下,最好使用FluentWait定义一个显式的等待,因为这将一次又一次地尝试查找元素,直到找到它或直到最后一个计时器用完为止。WebDriverWait是FluentWait的一种类型,因为WebDriverWait扩展了FluentWait并具有FluentWait类的所有功能,例如能够调整DOM轮询间隔、忽略异常

FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(timeoutSeconds, TimeUnit.SECONDS)
            .pollingEvery(500, TimeUnit.MILLISECONDS)
            .ignoring(NoSuchElementException.class);
FluentWait wait=新FluentWait(驱动程序)
.withTimeout(timeoutSeconds,TimeUnit.SECONDS)
.pollingEvery(500,时间单位为毫秒)
.忽略(NoSuchElementException.class);
WebDriverWait wait = new WebDriverWait (driver, 20);
wait.until(ExpectedConditions.VisibilityofElementLocated(By.xpath(""//button[@value='Save Changes']"")));
new FluentWait<WebDriver>(driver).withTimeout(30, TimeUnit.SECONDS).pollingevery(10, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
driver.implicitly_wait(10)
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.implicitly_wait(5)
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.implicitly_wait(0)
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
   // Waiting 30 seconds for an element to be present on the page, checking
   // for its presence once every 5 seconds.
   Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(NoSuchElementException.class);

   WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
     public WebElement apply(WebDriver driver) {
       return driver.findElement(By.id("foo"));
     }
   });
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
.
.
.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element_xpath")));
element.click();