[Selenium]等待元素出现之后再消失,界面上的loading icon都属于这种类型,之前的方法总是卡死,换这种方法目前还好用的...

等待元素出现之后再消失,界面上的loading icon都属于这种类型,之前的方法总是卡死,换这种方法目前还好用的

/**
     * Check if the element present with customized timeout
     * @param driver
     * @param locator
     * @param errorMessage
	 * @return
	 */
	public Boolean waitUntilElementPresent(WebDriver driver,final By locator, long timeOutInSeconds) {
		Boolean isPresent = false;
		for(int i=0;i<timeOutInSeconds;i++){
			isPresent=SeleniumUtil.isElementPresent(driver,locator);
			if(true==isPresent){
				break;
			}
			else{
				SeleniumUtil.sleep(1);
				System.out.println("Wait no more than "+timeOutInSeconds+"s for loading icon display, wait "+(i+1)+"s");
			}
		}
		return isPresent;
	}

 

/**
     * Wait until the element not present with customized timeout
     * @param driver
     * @param locator
     * @param errorMessage
	 * @return
	 */
	public Boolean waitUntilElementNotPresent(WebDriver driver,final By locator, long timeOutInSeconds) {
		Boolean isPresent = true;
		for(int i=0;i<timeOutInSeconds;i++){
			isPresent=SeleniumUtil.isElementPresent(driver,locator);
			if(false==isPresent){
				break;
			}
			else{
				SeleniumUtil.sleep(1);
				System.out.println("Wait no more than "+timeOutInSeconds+"s for loading icon disappear, wait "+(i+1)+"s");
			}
		}
		return isPresent;
	}

 

public void waitForLoadingDoneInNewWebPage(WebDriver driver){
		By locator=By.cssSelector("div.x-mask-msg");
		Boolean isPresent = this.waitUntilElementPresent(driver, locator, 10);
		if(true==isPresent){
System.out.println("Loading icon display in new web page"); System.out.println("Wait for loading icon disappear in new web page"); Boolean stillPresent = this.waitUntilElementNotPresent(driver, locator, 120); if(false==stillPresent){ System.out.println("Loading icon disappear in new web page."); } } else{ System.out.println("Loading icon does'nt display in new web page."); } }

 

转载于:https://www.cnblogs.com/MasterMonkInTemple/p/4535849.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Selenium 中,没有内置的方法可以让程序在等待界面某个元素时不停地刷新界面Selenium等待机制是基于条件判断的,当条件满足时会继续执行下一步操作,否则会等待指定的时间后抛出超时异常。 如果你希望在等待元素时不停地刷新界面,你可以自己编写一个循环来实现。例如,你可以使用 `driver.refresh()` 方法来刷新页面,然后再次尝试查找元素。你可以设置一个最大重试次数,当达到最大重试次数时仍未找到元素,可以抛出异常或执行其他操作。 这里是一个示例代码片段,演示了如何在等待元素时不停地刷新界面: ```python from selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() def wait_for_element_with_refresh(selector, max_retries=10): retries = 0 while retries < max_retries: try: element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, selector))) return element except TimeoutException: driver.refresh() retries += 1 raise TimeoutException("Element not found after {} retries".format(max_retries)) element = wait_for_element_with_refresh("#myElement") ``` 请注意,不要无限制地进行刷新和重试,应该设置一个合理的最大重试次数,以避免进入死循环。此外,过度的刷新和重试可能会导致测试执行效率下降,因此请根据具体情况进行权衡和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值