Wait until element is found

I record my testcase using katalon automation recoreder, then i export when copy the code to Katalon Studio. when I run the code, i found error

Test Cases/TC-1 FAILED because (of) com.thoughtworks.selenium.SeleniumException: Element //ul[@id=‘menu’]/li[3]/a/span not found
I think it happened beacuse, the page not yet load, but next action is to click something in the page.
I have tried to add implicit wait using selenium
_
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)_

but found error like this

Test Cases/TC-1 FAILED because (of) Variable ‘TimeUnit’ is not defined for test case.

Please help to solve this issue
Thank you

Hi,

Import the following package

import java.util.concurrent.TimeUnit

Also you can use the explicit wait also . Refer to following code

public class waithelper {

    public void waitForElement(By locator){
        WebDriver driver = DriverFactory.getWebDriver();
        WebDriverWait wait = new WebDriverWait(driver, 60);
        wait.pollingEvery(250, TimeUnit.MILLISECONDS);
        wait.ignoring(ElementNotFoundException.class);
        wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
    }

}
2 Likes

use waitElementForClickable for first action after navigateurl if you want your code still run even script didn’t find the object just change failure handling

Rahul said:

Hi,

Import the following package

import java.util.concurrent.TimeUnit

Also you can use the explicit wait also . Refer to following code

public class waithelper {

    public void waitForElement(By locator){
        WebDriver driver = DriverFactory.getWebDriver();
        WebDriverWait wait = new WebDriverWait(driver, 60);
        wait.pollingEvery(250, TimeUnit.MILLISECONDS);
        wait.ignoring(ElementNotFoundException.class);
        wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
    }

}


how to include this code for all web elements??  
whether its custom keyword?  
  

I have created a fluent wait custom function, based on attribute selection(now css and xpath are supported
import the following libs in the custom function

import org.openqa.selenium.WebElement
import com.google.common.base.Function

public class waitfluent {
protected WebDriver driver
@Keyword
public void waitForElementvisible(TestObject to, String PropertyValue){
//get the locator value by find it from the testobject
String locator = to.findPropertyValue(PropertyValue)
System.out.println(‘check’ + locator)
driver = DriverFactory.getWebDriver()

	FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
			.withTimeout(60, TimeUnit.SECONDS)
			.pollingEvery(5, TimeUnit.SECONDS)
			.ignoring(WebElementNotFoundException.class)
	WebElement element = wait.until(new Function<WebDriver, WebElement>() {

				public WebElement apply(WebDriver driver) {
					WebElement element
                                            //check if the locator value is css or xpath
					if (PropertyValue.equals("css")){
						element = driver.findElement(By.cssSelector(locator))
					}
					else if (PropertyValue.equals("xpath")){
						element = driver.findElement(By.xpath(locator))
					}

					return element
				}
			})
}

}

good luck :slight_smile:

1 Like

Hi Ralph,
I m looking for exact opposite behavior, wait until element is not found or disappear.
I know I can use selenium invisibilityOfElementLocated method. But in my scenario, an element is not present in DOM. Its get added to DOM runtime on performing any action and once it finishes, it removes that element from DOM.
For Example, on the page there are two buttons, let’s say button1 and button2. On clicking button1, it disables the screen for some time(don’t want to use explicit delay here). So this blocking me to click or select any other elements on the page.

I would really appreciate the help.
Thanks

Thanks Ralph, It works

Facing Issue when using Fluent Wait

The below is the Keyword :
package qm.webmail
import org.openqa.selenium.By
import org.openqa.selenium.Keys
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.FluentWait
import org.openqa.selenium.support.ui.Wait
import org.openqa.selenium.support.ui.WebDriverWait;
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.google.common.base.Function

import browser.Screen

public class Accept
{

@Keyword
public static void Quote(String quoteNo) 
{
	System.err.print(quoteNo)
	
	WebDriver driver = DriverFactory.getWebDriver();
	
	/*Waiting for the element, because the element will be visible after 50secs*/
	WebElement Quotexpath = fluentWait(By.xpath("//*[contains(text(),'"+quoteNo+"')]"));
		
	WebUI.delay(2)

	WebUI.setText(findTestObject('OutlookWebMail/SearchField'), quoteNo)

	WebUI.sendKeys(findTestObject('OutlookWebMail/SearchField'), Keys.chord(Keys.ENTER))

	WebUI.waitForPageLoad(60)

	WebElement Element = driver.findElements(By.xpath("//*[contains(text(),'"+quoteNo+"')]"))
	Element.click()

	WebUI.waitForPageLoad(60)

	WebUI.scrollToElement(findTestObject('OutlookWebMail/Hyperlink'), 5)
	
	WebUI.focus(findTestObject('OutlookWebMail/Hyperlink'))

	WebUI.delay(2)

	String ReviewAndAcceptUrl =	WebUI.getAttribute(findTestObject('OutlookWebMail/Hyperlink'), 'href')

	println (ReviewAndAcceptUrl)

	WebUI.navigateToUrl(ReviewAndAcceptUrl)

	WebUI.delay(4)

	WebUI.waitForPageLoad(300)

	WebUI.waitForElementClickable(findTestObject('OutlookWebMail/Accept'), 30)

	WebUI.click(findTestObject('OutlookWebMail/Accept'))     // Approve MQ

	WebUI.setText(findTestObject('OutlookWebMail/CustomerComments'), 'AUTOMATION TEST')

	WebUI.delay(1)

	WebUI.click(findTestObject('OutlookWebMail/Accept'))     // Approve MQ

	WebUI.delay(3)

}

public WebElement fluentWait(final By locator) 
{
	Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
			.withTimeout(120, TimeUnit.SECONDS)
			.pollingEvery(15, TimeUnit.SECONDS)
			.ignoring(NoSuchElementException.class);

	WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
		public WebElement apply(WebDriver driver) {
			return driver.findElement(locator);
		}
	});

	return  foo;
};

}

:::::Script::::

::::::ERROR ::::::: I’m Facing when I am running the script