简体   繁体   English

Selenium和Thread.Sleep中的Wait方法之间的区别

[英]Difference between Wait methods in Selenium and Thread.Sleep

Can please help me to understand the difference between below three waiting methods and efficient use in the program, 能否请我了解以下三种等待方式与程序中有效使用之间的区别,

  1. Thread.sleep(5000); 了Thread.sleep(5000);

  2. driver.manage().timeouts().implicit Wait(5, TimeUnit.SECONDS); driver.manage()。timeouts()。隐式Wait(5,TimeUnit.SECONDS);

  3. ExplicitWait(); ExplicitWait();


Thanks 谢谢

Blocking the thread is really a block java uses for example to control multithreaded application. 阻塞线程实际上是Java用于控制多线程应用程序的一个块。

An explicit waits 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. 最糟糕的情况是Thread.sleep(),它将条件设置为要等待的确切时间段。 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. WebDriverWait与ExpectedCondition结合是实现此目的的一种方法。

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. 隐式等待是告诉WebDriver在尝试查找不立即可用的一个或多个元素时,在一定时间内轮询DOM。 The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance. 默认设置为0。设置后,将在WebDriver对象实例的生存期内设置隐式等待。

You should not use Java's thread.sleep as a replacement for the selenium once, as you could potentially create multithreading errors depending on what else you do within the application. 您不应一次使用Java的thread.sleep来代替硒,因为您可能会根据应用程序中的其他操作而产生多线程错误。 (Deadlocks for example, or race conditions) (例如,死锁或比赛条件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM