Selenium (Java) - explicit and implicit wait wait

https://blog.csdn.net/yanhongyu315/article/details/80701005

In automated testing, sometimes only after the end of a task you need to wait for the next task, so we need to wait for some interval of time between the two tasks, and Selenium provides two methods, when we write automated test code, our most Do not use the good Thread.Sleep (), because in this way need to wait for a certain time.

An explicit wait

WebDriver driver = new ChromeDriver();
driver.get("https://www.baidu.com");
WebElement element = (new WebDriverWait(driver,5)).until(ExpectedConditions.presenceOfElementLocated(By.id("kw")));

The above third statement said that in 0-5s to locate the id is "kw" elements, WebDriverWait default would be called once every 500ms until ExpectedCondition positioning success or deadline, ExpectedCondition return value is either true or is not empty objects within the specified time if there is no successful positioning element, until () throws org.openqa.selenium.TimeoutException.
Second, implicit wait

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://www.baidu.com");
WebElement element1 = ((ChromeDriver) driver).findElementById("kw");

The above first statement said that if the element is not immediately able to locate successful, the search will go WebDriver DOM element positioned within 5s period, after this time, once set its range is WebDriver entire life cycle, if not within the specified time positioning is successful, it will throw org.openqa.selenium.NoSuchElementException.

[Note] display and implicit best not to mix, then mix may cause unpredictable waiting time, for example, set up an implicit wait 10s, it shows the wait 15s, 20s might occur after a timeout
               in need of special note is : hidden waiting all work for the entire period of the driver, so just set once

Guess you like

Origin www.cnblogs.com/person008/p/10980168.html