在 Selenium WebDriver 中检查元素是否可点击
我们可以使用同步检查元素在 Selenium webdriver 中是否可点击。在同步中,有一个显式等待,其中驱动程序会等待元素的预期条件满足。
要验证元素是否可点击,我们应使用 elementToBeClickable 条件。如果在驱动程序等待时间内元素的标准不满足,则会抛出一个超时异常。
语法
WebDriverWait wt = new WebDriverWait(driver, 5);
wt.until(ExpectedConditions.elementToBeClickable (By.className("s-buy")));我们必须添加 - import org.openqa.selenium.support.ui.ExpectedConditions 和 import org.openqa.selenium.support.ui.WebDriverWait 语句,以便在我们的代码中实现 ExpectedConditions。
示例
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 ElemntClickable{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//implicit wait
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//URL launch
driver.get("https://tutorialspoint.com/index.htm");
WebElement n=driver.findElement(By.className("mui-btn"));
n.click();
// explicit wait
WebDriverWait wt = new WebDriverWait(driver,6);
// elementToBeClickable expected criteria
wt.until(ExpectedConditions.elementToBeClickable (By.className("s-buy")));
System.out.println("Current page title:" + driver.getTitle());
driver.close();
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP