Best Selenium code snippet using org.openqa.selenium.support.ui.FluentWait.pollingEvery
Source:PollingUtils (2).java  
...27		int pollingInterval = 1;28		int timeOut = 60;2930		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)31				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))32				.ignoring(NoSuchElementException.class);3334		try {35			System.out.println(36					"Find the ELement " + by + " with timeOut:pollingInterval as " + timeOut + ":" + pollingInterval);37			wait.until(ExpectedConditions.presenceOfElementLocated(by));38		} catch (TimeoutException e) {39			StackTraceElement[] a = e.getStackTrace();40			StackTraceElement i = a[3];41			String exceptionMessage = "TimeoutException for element :" + by + ". Found in the code at Class: "42					+ i.getClassName() + " | Method: " + i.getMethodName() + " | Line_Number: " + i.getLineNumber();43			throw new TimeoutException(exceptionMessage, e);44		}4546		WebElement element = webDriver.findElement(by);47		return element;48	}4950	public WebElement getElementBy(final WebDriver webDriver, final By by, int timeOut, int pollingInterval)51			throws TimeoutException {52		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)53				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))54				.ignoring(NoSuchElementException.class);5556		try {57			System.out.println(58					"Find the ELement " + by + " with timeOut:pollingInterval as " + timeOut + ":" + pollingInterval);59			wait.until(ExpectedConditions.presenceOfElementLocated(by));60		} catch (TimeoutException e) {61			StackTraceElement[] a = e.getStackTrace();62			StackTraceElement i = a[3];63			String exceptionMessage = "TimeoutException for element :" + by + ". Found in the code at Class: "64					+ i.getClassName() + " | Method: " + i.getMethodName() + " | Line_Number: " + i.getLineNumber();65			throw new TimeoutException(exceptionMessage, e);66		}6768		WebElement element = webDriver.findElement(by);69		return element;7071	}7273	public List<WebElement> getElementListBy(final WebDriver webDriver, final By by, int timeOut, int pollingInterval)74			throws TimeoutException {75		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)76				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))77				.ignoring(NoSuchElementException.class);7879		try {80			System.out.println(81					"Find the ELement " + by + " with timeOut:pollingInterval as " + timeOut + ":" + pollingInterval);82			wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(by));83		} catch (TimeoutException e) {84			StackTraceElement[] a = e.getStackTrace();85			StackTraceElement i = a[3];86			String exceptionMessage = "TimeoutException for element :" + by + ". Found in the code at Class: "87					+ i.getClassName() + " | Method: " + i.getMethodName() + " | Line_Number: " + i.getLineNumber();88			throw new TimeoutException(exceptionMessage, e);89		}9091		List<WebElement> elementList = webDriver.findElements(by);92		return elementList;93	}9495	public List<WebElement> getElementListBy(final WebDriver webDriver, final By by) throws TimeoutException {96		int pollingInterval = 1;97		int timeOut = 60;98		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)99				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))100				.ignoring(NoSuchElementException.class);101102		try {103			System.out.println(104					"Find the ELement " + by + " with timeOut:pollingInterval as " + timeOut + ":" + pollingInterval);105			wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(by));106		} catch (TimeoutException e) {107			StackTraceElement[] a = e.getStackTrace();108			StackTraceElement i = a[3];109			String exceptionMessage = "TimeoutException for element :" + by + ". Found in the code at Class: "110					+ i.getClassName() + " | Method: " + i.getMethodName() + " | Line_Number: " + i.getLineNumber();111			throw new TimeoutException(exceptionMessage, e);112		}113114		List<WebElement> elementList = webDriver.findElements(by);115		return elementList;116	}117118	public List<WebElement> getElementListIfPresent(final WebDriver webDriver, final By by) {119		int pollingInterval = 1;120		int timeOut = 10;121		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)122				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))123				.ignoring(NoSuchElementException.class);124		try {125			System.out.println(126					"Find the ELement " + by + " with timeOut:pollingInterval as " + timeOut + ":" + pollingInterval);127			wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(by));128		} catch (TimeoutException e) {129130		}131		List<WebElement> elementList = webDriver.findElements(by);132		return elementList;133134	}135136	public List<WebElement> getElementListIfPresent(final WebDriver webDriver, final By by, int timeOut,137			int pollingInterval) {138		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)139				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))140				.ignoring(NoSuchElementException.class);141		try {142			System.out.println(143					"Find the ELement " + by + " with timeOut:pollingInterval as " + timeOut + ":" + pollingInterval);144			wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(by));145		} catch (TimeoutException e) {146147		}148		List<WebElement> elementList = webDriver.findElements(by);149		return elementList;150151	}152153	public List<WebElement> getElementListBy(boolean checkEmptyNess, final WebDriver webDriver, final By by,154			int timeOut, int pollingInterval) {155156		if (checkEmptyNess) {157158			org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)159					.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))160					.ignoring(NoSuchElementException.class);161162			try {163				System.out.println("Find the Element " + by + " with timeOut:pollingInterval as " + timeOut + ":"164						+ pollingInterval);165				wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(by));166			} catch (TimeoutException e) {167168				List<WebElement> elementList = new ArrayList<WebElement>();169				return elementList;170171			}172		}173		List<WebElement> elementList = webDriver.findElements(by);174		return elementList;175	}176177	public List<WebElement> getElementListBy(boolean checkEmptyNess, final WebDriver webDriver, final By by) {178		int pollingInterval = 1;179		int timeOut = 60;180181		if (checkEmptyNess) {182183			org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)184					.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))185					.ignoring(NoSuchElementException.class);186187			try {188				System.out.println("Find the Element " + by + " with timeOut:pollingInterval as " + timeOut + ":"189						+ pollingInterval);190				wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(by));191			} catch (TimeoutException e) {192193				List<WebElement> elementList = new ArrayList<WebElement>();194				return elementList;195196			}197		}198		List<WebElement> elementList = webDriver.findElements(by);199		return elementList;200	}201202	public void waitForElementPresence(WebDriver webDriver, By by, int timeOut, int pollingInterval)203			throws TimeoutException {204		if (webDriver == null) {205			throw new IllegalArgumentException("The WebDriver cannot be null.");206		}207208		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)209				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))210				.ignoring(NoSuchElementException.class);211212		try {213			wait.until(ExpectedConditions.presenceOfElementLocated(by));214		} catch (TimeoutException e) {215			StackTraceElement[] a = e.getStackTrace();216			StackTraceElement i = a[3];217			String exceptionMessage = "TimeoutException for element :" + by + ". Found in the code at Class: "218					+ i.getClassName() + " | Method: " + i.getMethodName() + " | Line_Number: " + i.getLineNumber();219			throw new TimeoutException(exceptionMessage, e);220		}221222	}223224	public boolean waitForElementPresence(Boolean ignoreExceptionThrow, WebDriver webDriver, By by, int timeOut,225			int pollingInterval) throws TimeoutException {226		if (webDriver == null) {227			throw new IllegalArgumentException("The WebDriver cannot be null.");228		}229		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)230				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))231				.ignoring(NoSuchElementException.class);232233		if (ignoreExceptionThrow) {234			try {235			} catch (TimeoutException e) {236				System.out.println(237						"Does not Throw TimeoutException as this UTIL does not has that feature. Use other Function to Throw TimeoutException.");238				return false;239			}240		} else {241			wait.until(ExpectedConditions.presenceOfElementLocated(by));242		}243		return true;244245	}246247	public void waitForElementPresence(WebDriver webDriver, By by) throws TimeoutException {248		int pollingInterval = 1;249		int timeOut = 60;250		if (webDriver == null) {251			throw new IllegalArgumentException("The WebDriver cannot be null.");252		}253254		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)255				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))256				.ignoring(NoSuchElementException.class);257258		try {259			wait.until(ExpectedConditions.presenceOfElementLocated(by));260		} catch (TimeoutException e) {261			StackTraceElement[] a = e.getStackTrace();262			StackTraceElement i = a[3];263			String exceptionMessage = "TimeoutException for element Clickable Search :" + by264					+ ". Found in the code at Class: " + i.getClassName() + " | Method: " + i.getMethodName()265					+ " | Line_Number: " + i.getLineNumber();266			throw new TimeoutException(exceptionMessage, e);267		}268269	}270271	public void waitForElementtobeClickable(WebDriver webDriver, By by, int timeOut, int pollingInterval)272			throws TimeoutException {273		if (webDriver == null) {274			throw new IllegalArgumentException("The WebDriver cannot be null.");275		}276277		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)278				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))279				.ignoring(NoSuchElementException.class);280281		try {282			wait.until(ExpectedConditions.elementToBeClickable(by));283		} catch (TimeoutException e) {284			StackTraceElement[] a = e.getStackTrace();285			StackTraceElement i = a[3];286			String exceptionMessage = "TimeoutException for  element Clickable Search :" + by287					+ ". Found in the code at Class: " + i.getClassName() + " | Method: " + i.getMethodName()288					+ " | Line_Number: " + i.getLineNumber();289			throw new TimeoutException(exceptionMessage, e);290		}291292	}293294	public void waitForElementtobeClickable(WebDriver webDriver, By by) throws TimeoutException {295		int pollingInterval = 1;296		int timeOut = 60;297		if (webDriver == null) {298			throw new IllegalArgumentException("The WebDriver cannot be null.");299		}300301		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)302				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))303				.ignoring(NoSuchElementException.class);304305		try {306			wait.until(ExpectedConditions.elementToBeClickable(by));307		} catch (TimeoutException e) {308			StackTraceElement[] a = e.getStackTrace();309			StackTraceElement i = a[3];310			String exceptionMessage = "TimeoutException for element Clickable Search: " + by311					+ ". Found in the code at Class: " + i.getClassName() + " | Method: " + i.getMethodName()312					+ " | Line_Number: " + i.getLineNumber();313			throw new TimeoutException(exceptionMessage, e);314		}315	}316317	public void waitForElementtobeClickable2(WebDriver webDriver, WebElement by) throws TimeoutException {318		int pollingInterval = 1;319		int timeOut = 60;320321		if (webDriver == null) {322			throw new IllegalArgumentException("The WebDriver cannot be null.");323		}324325		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)326				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))327				.ignoring(NoSuchElementException.class, StaleElementReferenceException.class);328329		try {330			wait.until(ExpectedConditions.elementToBeClickable(by));331		} catch (TimeoutException e) {332			StackTraceElement[] a = e.getStackTrace();333			StackTraceElement i = a[3];334			String exceptionMessage = "TimeoutException for element Clickable Search: " + by335					+ ". Found in the code at Class: " + i.getClassName() + " | Method: " + i.getMethodName()336					+ " | Line_Number: " + i.getLineNumber();337			throw new TimeoutException(exceptionMessage, e);338		}339	}340341	public void waitForElementPresenceByExcpectedCondition(WebDriver webDriver,342			ExpectedCondition<WebElement> expectedCondMethodAndLocator) throws TimeoutException {343		int pollingInterval = 1;344		int timeOut = 60;345		if (webDriver == null) {346			throw new IllegalArgumentException("The WebDriver cannot be null.");347		}348349		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)350				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))351				.ignoring(NoSuchElementException.class);352353		try {354			wait.until(expectedCondMethodAndLocator);355		} catch (TimeoutException e) {356			StackTraceElement[] a = e.getStackTrace();357			StackTraceElement i = a[3];358			String exceptionMessage = "TimeoutException for ExpectedCondition for Element Search. Found in the code at Class: "359					+ i.getClassName() + " | Method: " + i.getMethodName() + " | Line_Number: " + i.getLineNumber();360			throw new TimeoutException(exceptionMessage, e);361		}362363	}364365	public void checkPresenceOfAllElementsLocatedBy(WebDriver webDriver,366			ExpectedCondition<List<WebElement>> expectedCondMethodAndLocator) throws TimeoutException {367		int pollingInterval = 1;368		int timeOut = 60;369		if (webDriver == null) {370			throw new IllegalArgumentException("The WebDriver cannot be null.");371		}372373		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)374				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))375				.ignoring(NoSuchElementException.class);376377		try {378			wait.until(expectedCondMethodAndLocator);379		} catch (TimeoutException e) {380			StackTraceElement[] a = e.getStackTrace();381			StackTraceElement i = a[3];382			String exceptionMessage = "TimeoutException for ExpectedCondition for Element List Search. Found in the code at Class: "383					+ i.getClassName() + " | Method: " + i.getMethodName() + " | Line_Number: " + i.getLineNumber();384			throw new TimeoutException(exceptionMessage, e);385		}386	}387388	public void checkTextToBePresentInElementLocated(WebDriver webDriver, By by, String textValue)389			throws TimeoutException {390		int pollingInterval = 1;391		int timeOut = 60;392393		if (webDriver == null) {394			throw new IllegalArgumentException("The WebDriver cannot be null.");395		}396397		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)398				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))399				.ignoring(NoSuchElementException.class);400		ExpectedCondition<Boolean> expectedCondMethodAndLocator = ExpectedConditions.textToBePresentInElementLocated(by,401				textValue);402403		try {404			wait.until(expectedCondMethodAndLocator);405		} catch (TimeoutException e) {406			StackTraceElement[] a = e.getStackTrace();407			StackTraceElement i = a[3];408			String exceptionMessage = "TimeoutException for ExpectedCondition for Element Search. Found in the code at Class: "409					+ i.getClassName() + " | Method: " + i.getMethodName() + " | Line_Number: " + i.getLineNumber();410411			throw new TimeoutException(exceptionMessage, e);412		}413	}414415	public void checkForFrameToBeAvailableAndSwitchToIt(WebDriver webDriver, By by) throws TimeoutException {416		int pollingInterval = 1;417		int timeOut = 40;418		if (webDriver == null) {419			throw new IllegalArgumentException("The WebDriver cannot be null.");420		}421422		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)423				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))424				.ignoring(NoSuchElementException.class);425426		ExpectedCondition<WebDriver> expectedCondition = ExpectedConditions.frameToBeAvailableAndSwitchToIt(by);427428		try {429			wait.until(expectedCondition);430		} catch (TimeoutException e) {431			StackTraceElement[] a = e.getStackTrace();432			StackTraceElement i = a[3];433			String exceptionMessage = "TimeoutException for ExpectedCondition for Element Search. Found in the code at Class: "434					+ i.getClassName() + " | Method: " + i.getMethodName() + " | Line_Number: " + i.getLineNumber();435			throw new TimeoutException(exceptionMessage, e);436437		}438	}439440	public void checkForElementStaleness(WebDriver webDriver, By by, int timeOut, int pollingInterval)441			throws TimeoutException {442		if (webDriver == null) {443			throw new IllegalArgumentException("The WebDriver cannot be null.");444		}445446		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)447				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))448				.ignoring(NoSuchElementException.class);449450		WebElement element = getElementBy(webDriver, by);451		ExpectedCondition<Boolean> expectedCondition = ExpectedConditions.stalenessOf(element);452453		try {454			wait.until(expectedCondition);455		} catch (TimeoutException e) {456			StackTraceElement[] a = e.getStackTrace();457			StackTraceElement i = a[3];458			String exceptionMessage = "TimeoutException for ExpectedCondition for Element Search. Found in the code at Class: "459					+ i.getClassName() + " | Method: " + i.getMethodName() + " | Line_Number: " + i.getLineNumber();460			throw new TimeoutException(exceptionMessage, e);461		}462	}463464	public void checkForElementStaleness(WebDriver webDriver, WebElement element, int timeOut, int pollingInterval)465			throws TimeoutException {466		if (webDriver == null) {467			throw new IllegalArgumentException("The WebDriver cannot be null.");468		}469470		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)471				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))472				.ignoring(NoSuchElementException.class);473474		ExpectedCondition<Boolean> expectedCondition = ExpectedConditions.stalenessOf(element);475476		try {477			wait.until(expectedCondition);478		} catch (TimeoutException e) {479			StackTraceElement[] a = e.getStackTrace();480			StackTraceElement i = a[3];481			String exceptionMessage = "TimeoutException for ExpectedCondition for Element Search. Found in the code at Class: "482					+ i.getClassName() + " | Method: " + i.getMethodName() + " | Line_Number: " + i.getLineNumber();483			throw new TimeoutException(exceptionMessage, e);484		}485486	}487488	public void checkForElementStaleness(WebDriver webDriver, WebElement element) throws TimeoutException {489		int pollingInterval = 1;490		int timeOut = 60;491		if (webDriver == null) {492			throw new IllegalArgumentException("The WebDriver cannot be null.");493		}494495		org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver)496				.withTimeout(Duration.ofSeconds(timeOut)).pollingEvery(Duration.ofSeconds(pollingInterval))497				.ignoring(NoSuchElementException.class);498499		ExpectedCondition<Boolean> expectedCondition = ExpectedConditions.stalenessOf(element);500501		try {502			wait.until(expectedCondition);503		} catch (TimeoutException e) {504			StackTraceElement[] a = e.getStackTrace();505			StackTraceElement i = a[3];506			String exceptionMessage = "TimeoutException for ExpectedCondition for Element Search. Found in the code at Class: "507					+ i.getClassName() + " | Method: " + i.getMethodName() + " | Line_Number: " + i.getLineNumber();508			throw new TimeoutException(exceptionMessage, e);509		}510
...Source:WebElementManager.java  
...50      }51    };52    new FluentWait<>(driver)53        .withTimeout(Duration.ofSeconds(30))54        .pollingEvery(Duration.ofMillis(50))55        .until(pendingHttpCallsCondition);56  }57  public String getTextFromElement(By locator) {58    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)59        .withTimeout(Duration.ofSeconds(30))60        .pollingEvery(Duration.ofMillis(50))61        .ignoring(NoSuchElementException.class);62    wait.until(new Function<WebDriver, ExpectedCondition<WebElement>>() {63      public ExpectedCondition<WebElement> apply(WebDriver driver) {64        return ExpectedConditions.elementToBeClickable(driver.findElement(locator));65      }66    });67    return driver.findElement(locator).getText();68  }69  public void fillInText(By locator, String text) {70    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)71        .withTimeout(Duration.ofSeconds(30))72        .pollingEvery(Duration.ofMillis(50))73        .ignoring(NoSuchElementException.class);74    WebElement element = wait.until(new Function<WebDriver, WebElement>() {75      public WebElement apply(WebDriver driver) {76        return driver.findElement(locator);77      }78    });79    element.sendKeys(text);80    /**81     * verify if the field was filled in.82     */83    String actualTextInTheTextFiled = element.getAttribute("value");84    Assert.assertTrue(actualTextInTheTextFiled.equals(text));85  }86  public void pressOnBtn(By locator) {87    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)88        .withTimeout(Duration.ofSeconds(30))89        .pollingEvery(Duration.ofMillis(50))90        .ignoring(NoSuchElementException.class);91    wait.until(new Function<WebDriver, WebElement>() {92      public WebElement apply(WebDriver driver) {93        return driver.findElement(locator);94      }95    }).click();96  }97  public void waitForElementIsNotPresentOnPageAnymore(By locator) {98    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)99        .withTimeout(Duration.ofSeconds(5))100        .pollingEvery(Duration.ofMillis(50))101        .ignoring(NoSuchElementException.class);102    wait.until(new Function<WebDriver, ExpectedCondition<Boolean>>() {103      public ExpectedCondition<Boolean> apply(WebDriver driver) {104        return ExpectedConditions.invisibilityOf(driver.findElement(locator));105      }106    });107  }108  public WebElement getClickableElement(By locator) {109    return new FluentWait<>(driver)110        .withTimeout(Duration.ofSeconds(10))111        .pollingEvery(Duration.ofMillis(50))112        .ignoring(NoSuchElementException.class)113        .until(ExpectedConditions.elementToBeClickable(locator));114  }115  public List<WebElement> getClickableElements(By locator) {116    return new FluentWait<>(driver)117        .withTimeout(Duration.ofSeconds(30))118        .pollingEvery(Duration.ofMillis(50))119        .ignoring(NoSuchElementException.class)120        .until(driver -> driver.findElements(locator));121  }122  public void waitForAttributeValue(By locator, String attributeName, String value) {123    new FluentWait<>(driver)124        .withTimeout(Duration.ofSeconds(30))125        .pollingEvery(Duration.ofMillis(50))126        .ignoring(NoSuchElementException.class)127        .until(ExpectedConditions.attributeContains(locator, attributeName, value));128  }129  public void waitForAttributeValueNotPresent(By locator, String attributeName, String value) {130    new FluentWait<>(driver)131        .withTimeout(Duration.ofSeconds(10))132        .pollingEvery(Duration.ofMillis(50))133        .ignoring(NoSuchElementException.class).until((ExpectedCondition<Boolean>) driver -> {134          WebElement button = driver.findElement(locator);135          String enabled;136          try {137            enabled = button.getAttribute(attributeName);138          } catch (NullPointerException e) {139            enabled = "";140          }141          if (enabled != null) {142            if (enabled.equals(value))143              return false;144            else145              return true;146          }147          return true;148        });149  }150  public Boolean waitForElementToBeDisplayed(By locator) {151    return new FluentWait<>(driver)152        .withTimeout(Duration.ofSeconds(30))153        .pollingEvery(Duration.ofMillis(50))154        .ignoring(NoSuchElementException.class)155        .until(driver -> driver.findElement(locator).isDisplayed());156  }157  public Boolean isAllImagesLoaded(WebElement element) {158    Object tmp = ((JavascriptExecutor) driver)159        .executeScript("return arguments[0].complete && typeof arguments[0].naturalWidth != " +160            "\"undefined\" && arguments[0].naturalWidth > 0", element);161    return true;162  }163}...Source:Topic_17_Wait_Element_Status_Part6_FluentWait.java  
...36	public void TC_01_FLuent() {37		driver.get("https://automationfc.github.io/fluent-wait/"); 38		fluentElement = new FluentWait<WebElement>(driver.findElement(By.id("javascript_countdown_time")));39		40		fluentElement.withTimeout(12, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);41		fluentElement.until(new Function<WebElement, Boolean>() {42			public Boolean apply(WebElement element) {43				// Kiá»m tra Äiá»u kiá»n countdown = 0044				boolean flag = element.getText().endsWith("02");45				System.out.println("Time = " + element.getText());46				// return giá trá» cho function apply47				return flag;48			}49		});50	}51	52//	public void TC_02_FLuentWait() {53//		driver.get("http://the-internet.herokuapp.com/dynamic_loading/2"); 54//		waitForElementAndClick(By.xpath("//div[@id='start']/button"));55//		Assert.assertTrue(waitForElementAndDisplayed(By.xpath("//div[@id='finish']/h4[text()='Hello World!']")));56//		57//	}58//	59//60//	public WebElement waitedElement(By locator) {61//		FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)62//				.withTimeout(timeout, TimeUnit.SECONDS)63//				.pollingEvery(interval, TimeUnit.SECONDS)64//				.ignoring(NoSuchElementException.class);65//		66//		WebElement element = wait.until(new Function<Webdriver, WebElement>(){67//			public WebElement apply(WebDriver driver) {68//				return driver.findElement(locator);69//			}70//		});71//		return element;72//	}73	74//	public void waitForElementAndClick(By locator) {75//		FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)76//				.withTimeout(timeout, TimeUnit.SECONDS)77//				.pollingEvery(interval, TimeUnit.SECONDS)78//				.ignoring(NoSuchElementException.class);79//		WebElement element = wait.until(new Function<Webdriver, WebElement>(){80//			public WebElement apply(WebDriver driver) {81//				return driver.findElement(locator);82//			}83//		});84//		element.click();85//	}86//	87//	public boolean waitForElementAndDisplayed(By locator) {88//		element = waitedElement(locator);89//		FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)90//				.withTimeout(timeout, TimeUnit.SECONDS)91//				.pollingEvery(interval, TimeUnit.SECONDS)92//				.ignoring(NoSuchElementException.class);93//		94//		boolean isDisplayed = wait.until(new Function<WebElement, Boolean>() {95//			public Boolean apply(WebElement element) {96//				boolean flag = element.isDisplayed();97//				return flag;98//			}99//		});100//		return isDisplayed;101//	}102	103	public String getDateTimeNow() {104		java.util.Date TimeNow = new java.util.Date();105		return TimeNow.toString();...Source:WebActionUtilities.java  
...25		WebElement element=null;26		try{27			logger.info("FluentWait Method started");28			Wait<RemoteWebDriver> fluentWait=new FluentWait<>(BaseDriverClass.getDriver())29					.pollingEvery(Duration.ofSeconds(1))30					.withTimeout(Duration.ofSeconds(15))31					.ignoring(NoSuchElementException.class);32			element=fluentWait.until(driver->driver.findElement(webLocator));33		}catch(Exception e){34			logger.fatal("Failure in Fluent Wait for Element method. Exception:"+e.getMessage());35		}36		logger.info("FluentWait Method Ended");37		return element;	38	};39	40	//Fluent Wait for Elements41	public static Function<By,List<WebElement>> fluentElementsWait=webLocator->{42		Wait<RemoteWebDriver> fluentWait=new FluentWait<>(BaseDriverClass.getDriver())43				.pollingEvery(Duration.ofSeconds(1))44				.withTimeout(Duration.ofSeconds(15))45				.ignoring(NoSuchElementException.class);46		return fluentWait.until(driver->driver.findElements(webLocator));47	};48	49	//Fluent or Explicit Wait for Visibility of Element50	public static Function<By,WebElement> waitForElementToBeVisible=webLocator->{51			Wait<RemoteWebDriver> wait=new FluentWait<>(BaseDriverClass.getDriver())52					.pollingEvery(Duration.ofSeconds(1))53					.withTimeout(Duration.ofSeconds(15))54					.ignoring(NoSuchElementException.class);55			return wait.until(ExpectedConditions.visibilityOfElementLocated(webLocator));56	};57	58	public static Function<By,Boolean> waitForElementToBeInVisible=webLocator->{59		Wait<RemoteWebDriver> wait=new FluentWait<>(BaseDriverClass.getDriver())60				.pollingEvery(Duration.ofSeconds(1))61				.withTimeout(Duration.ofSeconds(15))62				.ignoring(NoSuchElementException.class);63		return wait.until(ExpectedConditions.invisibilityOfElementLocated(webLocator));64	};65	66	public static BiConsumer<By,String> sendKeysToElement=new BiConsumer<By,String>(){67		public void accept(By webLocator, String textToEnter) {68			fluentElementWait.apply(webLocator).sendKeys(textToEnter);69		}70	};71	72	public static Consumer<By> clickOnElement=webLocator->fluentElementWait.apply(webLocator).click();73	74	public static BiConsumer<By,Object> selectFromDropDown=(webLocator,optionToSelect)-> {...Source:WaitFluent.java  
...9public class WaitFluent extends BrowserFunctions {10    public static void waitFluentPresenceClassName(String someclass) {11        Wait<WebDriver> wait1 = new FluentWait<WebDriver>(driver)12                .withTimeout(Duration.ofSeconds(60))13                .pollingEvery(Duration.ofSeconds(5))14                .ignoring(NoSuchElementException.class);15        wait1.until(ExpectedConditions.presenceOfElementLocated(By.className(someclass)));16    }17    public static void waitFluentPresenceXpath(String somexpath) {18        Wait<WebDriver> wait2 = new FluentWait<WebDriver>(driver)19                .withTimeout(Duration.ofSeconds(60))20                .pollingEvery(Duration.ofSeconds(5))21                .ignoring(NoSuchElementException.class);22        wait2.until(ExpectedConditions.presenceOfElementLocated(By.xpath(somexpath)));23    }24    public static void waitFluentPresenceCss(String somecss) {25        Wait<WebDriver> wait3 = new FluentWait<WebDriver>(driver)26                .withTimeout(Duration.ofSeconds(60))27                .pollingEvery(Duration.ofSeconds(5))28                .ignoring(NoSuchElementException.class);29        wait3.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(somecss)));30    }31    public static void waitFluentClicableClassName(String someclass) {32        Wait<WebDriver> wait4 = new FluentWait<WebDriver>(driver)33                .withTimeout(Duration.ofSeconds(60))34                .pollingEvery(Duration.ofSeconds(5))35                .ignoring(NoSuchElementException.class);36        wait4.until(ExpectedConditions.elementToBeClickable(By.className(someclass)));37    }38    public static void waitFluentClicableXpath(String someclass) {39        Wait<WebDriver> wait5 = new FluentWait<WebDriver>(driver)40                .withTimeout(Duration.ofSeconds(10))41                .pollingEvery(Duration.ofSeconds(1))42                .ignoring(NoSuchElementException.class);43        wait5.until(ExpectedConditions.elementToBeClickable(By.xpath(someclass)));44    }45    public static void waitFluentClicableCss(String someclass) {46        Wait<WebDriver> wait6 = new FluentWait<WebDriver>(driver)47                .withTimeout(Duration.ofSeconds(10))48                .pollingEvery(Duration.ofSeconds(1))49                .ignoring(NoSuchElementException.class);50        wait6.until(ExpectedConditions.elementToBeClickable(By.cssSelector(someclass)));51    }52}...Source:MyPersonalisDispalyElement.java  
...16    public WebElement customWaitBy(WebDriver  driver, By by){17        count++;18        try{19            driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);20            FluentWait  wait = new FluentWait(driver).withTimeout(10,TimeUnit.SECONDS).pollingEvery(200,TimeUnit.MILLISECONDS).ignoring(NoSuchElementException.class);21           element = (WebElement)wait.until(ExpectedConditions.visibilityOfElementLocated(by));22          //  wait.until(ExpectedConditions.);23           new  Tool_Thread_Wait().Thread_Wait(1, 500);24         if( element.isDisplayed()){25            // new  Tool_Thread_Wait().Thread_Wait(1, 100);26             element_personal=element;27             //return element_personal;28          }29            else{30             driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);31            WebElement element_vis= driver.findElement(by);32             FluentWait  wait_two = new FluentWait(driver).withTimeout(10,TimeUnit.SECONDS).pollingEvery(200,TimeUnit.MILLISECONDS).ignoring(NoSuchElementException.class);33            wait_two.until(ExpectedConditions.visibilityOf(element_vis));34             element_personal=element_vis;35             return element_personal;36         }37        } catch (StaleElementReferenceException ex){38              new  Tool_Thread_Wait().Thread_Wait(1, 3000);39             if (count<4) {40                 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);41                 customWaitBy(driver, by);42             }43            else{44                 FluentWait wait_inexcept = new FluentWait(driver);45                 wait_inexcept.withTimeout(5000, TimeUnit.MILLISECONDS);46                 wait_inexcept.pollingEvery(250, TimeUnit.MILLISECONDS);47                 wait_inexcept.ignoring(NoSuchElementException.class);48                element2= (WebElement) wait_inexcept.until(ExpectedConditions.visibilityOf(driver.findElement(by)));49                 element_personal = element2;50                 return element_personal;51             }52//return null;53        }54       return  element_personal;55    }56}...Source:BasePage.java  
...35    public void waitForElement(By element)36    {37        FluentWait fluentWait = new FluentWait(getDriver());38        fluentWait.withTimeout(100000, TimeUnit.MILLISECONDS);39        fluentWait.pollingEvery(250, TimeUnit.MILLISECONDS);40        fluentWait.ignoring(NoSuchElementException.class);41        fluentWait.until(ExpectedConditions.visibilityOfElementLocated(element));42    }43    public boolean waitForNewTab(String windowTitle) {44        boolean windowFound = false;45        FluentWait fluentWait = new FluentWait(getDriver());46        fluentWait.withTimeout(5000, TimeUnit.MILLISECONDS);47        fluentWait.pollingEvery(250, TimeUnit.MILLISECONDS);48        Set<String> handle = getDriver().getWindowHandles();49        if(handle.size() > 1)50        {51            for(String windowHandle: handle)52            {53                if (getDriver().switchTo().window(windowHandle).getTitle().equals(windowTitle)) {54                    windowFound = true;55                    break;56                }57            }58        }59        return windowFound;60    }61    public void waitForJsToLoad()...Source:FluentWaitConcept.java  
...23		By loginBtn = By.xpath("//input[@value='Login']");24		25//		Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)26//								.withTimeout(Duration.ofSeconds(10))27//								.pollingEvery(Duration.ofMillis(2000))28//								.ignoring(NoSuchElementException.class);29//		30//		wait.until(ExpectedConditions.presenceOfElementLocated(emailId)).sendKeys("naveen@gmail.com");31//		32		WebDriverWait wait = new WebDriverWait(driver, 10);33		wait.34		withTimeout(Duration.ofSeconds(10)).35		pollingEvery(Duration.ofMillis(2000)).36		ignoring(NoSuchElementException.class);37		38		wait.until(ExpectedConditions.presenceOfElementLocated(emailId)).sendKeys("naveen@gmail.com");39		40		41	}42	43	public static WebElement waitForElementWithFluentWait(By locator, int timeOut, long pollingTime) {44		Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)45				.withTimeout(Duration.ofSeconds(timeOut))46				.pollingEvery(Duration.ofMillis(pollingTime))47				.ignoring(NoSuchElementException.class);48		49		return wait.until(ExpectedConditions.presenceOfElementLocated(locator));50	}51	52	53	public static WebDriver waitForFrameWithFluentWait(By locator, int timeOut, long pollingTime) {54		Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)55				.withTimeout(Duration.ofSeconds(timeOut))56				.pollingEvery(Duration.ofMillis(pollingTime))57				.ignoring(NoSuchElementException.class);58		59		return wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(locator));60	}61	62	63	64	65}...pollingEvery
Using AI Code Generation
1import java.util.concurrent.TimeUnit;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.support.ui.FluentWait;7import org.openqa.selenium.support.ui.Wait;8import org.testng.annotations.Test;9import com.google.common.base.Function;10public class FluentWaitExample {11    public void test1() {12        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Vikas\\Downloads\\chromedriver_win32\\chromedriver.exe");13        WebDriver driver = new ChromeDriver();14        driver.manage().window().maximize();15        driver.findElement(By.linkText("Gmail")).click();16        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)17                .withTimeout(30, TimeUnit.SECONDS)18                .pollingEvery(5, TimeUnit.SECONDS)19                .ignoring(Exception.class);20        Function<WebDriver, WebElement> function = new Function<WebDriver, WebElement>() {21            public WebElement apply(WebDriver arg0) {22                WebElement element = arg0.findElement(By.id("identifierId"));23                String getTextOnPage = element.getText();24                if(getTextOnPage.equals("Enter your email or phone number")) {25                    System.out.println(getTextOnPage);26                    return element;27                }28                else {29                    System.out.println("FluentWait Failed");30                    return null;31                }32            }33        };34        WebElement element = wait.until(function);35        element.sendKeys("pollingEvery
Using AI Code Generation
1package com.automationrhapsody.selenium;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.support.ui.FluentWait;7import org.openqa.selenium.support.ui.Wait;8import org.openqa.selenium.support.ui.ExpectedConditions;9import java.time.Duration;10public class FluentWaitPollingEveryExample {11    public static void main(String[] args) throws InterruptedException {12        System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\Desktop\\Automation\\chromedriver.exe");13        WebDriver driver = new ChromeDriver();14        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)15                .withTimeout(Duration.ofSeconds(30))16                .pollingEvery(Duration.ofMillis(500));17        WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("button1")));18        wait = new FluentWait<WebDriver>(driver)19                .withTimeout(Duration.ofSeconds(30))20                .pollingEvery(Duration.ofMillis(500));21        element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("button1")));22        driver.quit();23    }24}LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!
