使用Selenium webdriver和java
例如,我们有很多字段,如下图所示 我尝试使用显式等待,如i)等待加载元素(但元素是动态加载的,直到我点击并等待几秒才填充)但是直到我暂停几秒钟才能工作。我使用以下方法
我正在创建一个框架,并想知道在其他组织中程序员是否确实使用类似的方法来暂停脚本或者是否未使用它?因为我需要这么用。
使用下面的方法,因为我不想使用Thread.sleep礼貌堆栈溢出。
public static void customewait(int seconds){
Date start = new Date();
Date end = new Date();
while(end.getTime() - start.getTime() < seconds * 1000){
end = new Date();
}
}
您可以使用selenium提供的等待如下:
WebDriverWait wait = new WebDriverWait(driver, 5000);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("idOfElement")));
此代码将等待元素在5000毫秒内可见,并在找到该元素时初始化对象。
5000是超时时间,以毫秒(ms)为单位
你可以在this link阅读更多内容
Thread.sleep将在给定时间内停止执行该线程,而Webdriver等待仅等待直到所述条件不满足。所以,最佳做法是使用网络驱动程序等待。
By ByLocator = By.xpath("Any XPath");
int seconds = 5 * 1000; // seconds in milli.
WebDriverWait wait = new WebDriverWait(driver, seconds);
wait.until(ExpectedConditions.presenceOfElementLocated(ByLocator));