การรออย่างชัดแจ้งจะมีผลกับองค์ประกอบเฉพาะในหน้าเว็บ จะหยุดดำเนินการจนกว่าเงื่อนไขจะเป็นที่พอใจ การรอแบบโจ่งแจ้งก็เป็นไดนามิกเช่นกัน เนื่องจากหากเวลารอคือสิบห้าวินาทีและเงื่อนไข (เช่น การรอให้องค์ประกอบคลิกได้ มองเห็นได้ หรือเลือกได้ เป็นต้น) สำเร็จก่อนเวลาที่กำหนดนี้ ตัวควบคุมจะย้ายไปยังขั้นตอนถัดไป .
Explicit wait สามารถปรับแต่งได้มากกว่า เนื่องจากเราสามารถตั้งค่าสำหรับเงื่อนไขได้ รายการของเงื่อนไขที่คาดหวังสำหรับการรออย่างชัดแจ้งมีการระบุไว้ด้านล่าง -
-
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();
}
}