[Selenium]怎样等待元素出现之后再消失,譬如Lo

系统 1632 0

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

这个是等多个loading icon出现后消失

      /**

	 * 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 出现后消失

      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.");

	}


    

 如果等显现的元素变成隐藏,相对要简单一些,先等它可见,再等它不可见

      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");

	}


    

 

[Selenium]怎样等待元素出现之后再消失,譬如Loading icon


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论