명시적 대기는 웹 페이지의 특정 요소에 적용됩니다. 조건이 충족될 때까지 실행을 일시 중지합니다. 명시적 대기는 대기 시간이 15초이고 이 지정된 시간 전에 조건(예:요소가 클릭 가능, 표시 또는 선택 가능하게 될 때까지 기다리는 것)이 충족되면 제어가 다음 단계로 이동하기 때문에 동적 대기이기도 합니다. .
명시적 대기는 조건에 맞게 설정할 수 있으므로 더 사용자 정의할 수 있습니다. 명시적 대기에 대한 일부 예상 조건 목록은 다음과 같습니다. -
-
textToBePresentInElement()
구문
w.until(ExpectedConditions.textToBePresentInElement(By.id(“<<id expression>>“), “Tutorialspoint”));
-
textToBeClickable()
구문
w.until(ExpectedConditions.textToBeClickable(By.id(“<<id expression>>“)));
-
alertisPresent()
구문
w.until(ExpectedConditions.alertisPresent())= null);
-
frameToBeAvailableAndSwitchToIt()
구문
w.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id(“<<frame id >>“)));
명시적은 구현 측면에서 복잡하지만 실행 속도에 영향을 미치지 않으며 페이지의 특정 요소에 적용됩니다.
명시적 대기에서 최대 시간이 지나면 ElementNotVisibleException이 발생합니다.
예시
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.Wait;
public class Explictwt {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://www.tutorialspoint.com/index.htm";
driver.get(url);
//implicit wait with time in seconds applied to each elements
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
//Clicking on Coding Ground link
driver.findElement(By.xpath("//span[text()=’Coding Ground’]")).click();
// explicit wait declaration
WebDriverWait w = new WebDriverWait(driver,10);
// condition to wait for with textToBePresentInElement method
w.until(ExpectedConditions.textToBePresentInElement(By.xpath("//img[@title=’Whiteboard’]"),” Whiteboard”));
driver.quit();
}
}