如何在 Selenium Webdriver 中使用 Java 中的 clickandwait?
我們可以在 Selenium 中點選並等待。這可以透過同步概念實現。我們將使用顯示等待條件並等待在下一步之前某個元素可點選。
顯示等待在丟擲異常之前等待指定時間。若要驗證某個元素是否可點選,我們可以使用期望條件elementToBeClickable。
示例
程式碼實現。
import org.openqa.selenium.By;
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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ElementClickableWait{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
String url = "https://tutorialspoint.tw/index.htm";
driver.get(url);
// identify element and click()
WebElement l=driver.findElement(By.className("mui-btn"));
l.click();
// explicit wait condition
WebDriverWait w = new WebDriverWait(driver,3);
// elementToBeClickable condition
w.until(ExpectedConditions
.elementToBeClickable (By.className("s-buy")));
// get page title of next page
System.out.println("Current page title:" + driver.getTitle());
driver.quit();
}
}輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP