[Selenium]怎樣等待元素出現以後再消失,譬如Loading icon

界面上有些元素是要先等它出現,再等它消失,譬如loading iconcss

這個是等多個loading icon出現後消失java

/**
	 * Wait for loading icon disappear in dialog and Widgets
	 * @author jz
	 */
	public void waitForLoadingIconDisappearInDialog(WebDriver driver) {
		System.out.println("Wait for loading icons display in dialog or widgets.");
		Function<WebDriver, Boolean> waitFn = new Function<WebDriver, Boolean>() {
			@Override
			public Boolean apply(WebDriver driver) {
				try {
					for (WebElement el : driver.findElements(By.cssSelector("div.win-bd-mask[style*='display: block'] div.win-bd-mask-waiting"))) {
						if (el.isDisplayed()) {
							System.out.println("Loading icon display in dialog or widgets.");
							return false;
						}
					}
				} 
				catch (Exception ex) {
					return true;
				}
				return true;
			}
		};

		WebDriverWait wait = new WebDriverWait(driver, 120, 1000);
		wait.withMessage("Loading icons should disppear in dialog or widgets in 120s");
		wait.until(waitFn);
		System.out.println("Loading icons disspear in dialog or widgets.");
	}

 這個是等一個loading icon 出現後消失web

public void waitForLoadingIconDisappearInNewWebPage(WebDriver driver) {
		System.out.println("Wait for loading icon display in new web page.");
		final By cssSelector=By.cssSelector("div.x-mask-msg");
		Function<WebDriver, Boolean> waitFn = new Function<WebDriver, Boolean>() {
			public Boolean apply(WebDriver driver) {
				try{
					if(driver.findElement(cssSelector).isDisplayed()){
						System.out.println("Loading icon display in new web page.");
						return false;
					}
				}
				catch(Exception e){
					return true;
				}
				return true;
			}
		};
		WebDriverWait wait = new WebDriverWait(driver, 120, 2000);
		wait.withMessage("Loading icon should disappear in new web page in 120s");
		wait.until(waitFn);
		System.out.println("Loading icon disappear in new web page.");
	}

 若是等顯現的元素變成隱藏,相對要簡單一些,先等它可見,再等它不可見app

public void waitForLoadingIconDisappearInHomePage(WebDriver driver) {
		this.waitForElementVisible(driver, By.cssSelector("div#window-waiting-box"), 20 ,"Waiting box should display in 20s");
		this.waitForElementNotVisible(driver, By.cssSelector("div#window-waiting-box"), 120 ,"Waiting box should disppear in 120s");
	}
相關文章
相關標籤/搜索