I'm using ExpectedConditions.invisibilityOf to check the invisibility of one of the element, but every time it throws the timeout exception for wait.until(ExpectedConditions.invisibilityOf(elementTobeInvisible));
OS: Windows 7
Selenium Version: 3.6.0 or 3.7.1
Browser: Chrome
Browser Version: 62.0.3202.94 (Official Build) (64-bit)
As a user, I want to make sure element is invisible (neither it is visible nor it has become stale) before proceeding to the next action in my automation script.
I have below custom wrapper method which accepts the element and waits for given time period for that element to become invisible. If element is still visible, it should throw an exception
protected void waitUntilElementGetsInvisible(WebElement elementTobeInvisible) {
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.invisibilityOf(elementTobeInvisible));
}
`Start of the code
public class TestForElementInvisibility {
public static WebDriver driver;
@FindBy(id = "some id")
public static WebElement element;
public static void main(String[] args)
{
try {
System.setProperty("webdriver.chrome.driver", "drivers/chromeDriver.exe");
ChromeOptions option = new ChromeOptions();
driver = new ChromeDriver(option);
PageFactory.initElements(driver, TestForElementInvisibility.class);
driver.navigate().to("https://www.google.com");
waitUntilElementGetsInvisible(element);
driver.findElement(By.name("q")).sendKeys("Some text");
}
finally {
driver.quit();
}
}
protected static void waitUntilElementGetsInvisible(WebElement element) {
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.invisibilityOf(element));
}
}
End of the code`
While using the wait.until(ExpectedConditions.invisibilityOf(elementTobeInvisible)); user should be getting true if the element is not visible on the page
While using the wait.until(ExpectedConditions.invisibilityOf(elementTobeInvisible)); user gets TimeoutException even though the element is not visible
_"C:\Program Files\Java\jdk1.8.0_111\bin\java" -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.3.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_111\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_111\jre\libdeploy.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\extdnsns.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\extnashorn.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\extzipfs.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_111\jre\libplugin.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\rt.jar;Z:functional-testtargettest-classes;Z:functional-testtarget\classes;C:\Users\userName.m2repository\com\google\guava\guava\23.0\guava-23.0.jar;C:\Users\userName.m2repository\com\google\codefindbugs\jsr305\1.3.9\jsr305-1.3.9.jar;C:\Users\userName.m2repository\com\google\errorprone\error_prone_annotations\2.0.18\error_prone_annotations-2.0.18.jar;C:\Users\userName.m2repository\com\google\j2objc\j2objc-annotations\1.1\j2objc-annotations-1.1.jar;C:\Users\userName.m2repository\org\codehaus\mojo\animal-sniffer-annotations\1.14\animal-sniffer-annotations-1.14.jar;C:\Users\userName.m2repository\org\hamcrest\hamcrest-all\1.3\hamcrest-all-1.3.jar;C:\Users\userName.m2repository\comthoughtworks\gauge\gauge-java\0.6.2\gauge-java-0.6.2.jar;C:\Users\userName.m2repository\org\reflections\reflections\0.9.10\reflections-0.9.10.jar;C:\Users\userName.m2repository\org\javassist\javassist\3.19.0-GA\javassist-3.19.0-GA.jar;C:\Users\userName.m2repository\com\google\codefindbugs\annotations\2.0.1\annotations-2.0.1.jar;C:\Users\userName.m2repository\com\github\javaparser\javaparser-core\2.3.0\javaparser-core-2.3.0.jar;C:\Users\userName.m2repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\userName.m2repository\org\hamcrest\hamcrest-library\1.3\hamcrest-library-1.3.jar;C:\Users\userName.m2repository\commons-io\commons-io\2.4\commons-io-2.4.jar;C:\Users\userName.m2repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;C:\Users\userName.m2repository\org\seleniumhq\selenium\selenium-java\3.7.1\selenium-java-3.7.1.jar;C:\Users\userName.m2repository\org\seleniumhq\selenium\selenium-api\3.7.1\selenium-api-3.7.1.jar;C:\Users\userName.m2repository\org\seleniumhq\selenium\selenium-chrome-driver\3.7.1\selenium-chrome-driver-3.7.1.jar;C:\Users\userName.m2repository\org\seleniumhq\selenium\selenium-edge-driver\3.7.1\selenium-edge-driver-3.7.1.jar;C:\Users\userName.m2repository\org\seleniumhq\selenium\selenium-firefox-driver\3.7.1\selenium-firefox-driver-3.7.1.jar;C:\Users\userName.m2repository\org\seleniumhq\selenium\selenium-ie-driver\3.7.1\selenium-ie-driver-3.7.1.jar;C:\Users\userName.m2repository\org\seleniumhq\selenium\selenium-opera-driver\3.7.1\selenium-opera-driver-3.7.1.jar;C:\Users\userName.m2repository\org\seleniumhq\selenium\selenium-remote-driver\3.7.1\selenium-remote-driver-3.7.1.jar;C:\Users\userName.m2repository\org\seleniumhq\selenium\selenium-safari-driver\3.7.1\selenium-safari-driver-3.7.1.jar;C:\Users\userName.m2repository\org\seleniumhq\selenium\selenium-support\3.7.1\selenium-support-3.7.1.jar;C:\Users\userName.m2repositorynet\bytebuddy\byte-buddy\1.7.5\byte-buddy-1.7.5.jar;C:\Users\userName.m2repository\org\apache\commons\commons-exec\1.3\commons-exec-1.3.jar;C:\Users\userName.m2repository\commons-codec\commons-codec\1.10\commons-codec-1.10.jar;C:\Users\userName.m2repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\userName.m2repository\com\google\code\gson\gson\2.8.2\gson-2.8.2.jar;C:\Users\userName.m2repository\org\apache\httpcomponents\httpclient\4.5.3\httpclient-4.5.3.jar;C:\Users\userName.m2repository\org\apache\httpcomponents\httpcore\4.4.6\httpcore-4.4.6.jar;C:\Users\userName.m2repository\com\jayway\restassured\rest-assured\2.9.0\rest-assured-2.9.0.jar;C:\Users\userName.m2repository\org\codehaus\groovy\groovy\2.4.4\groovy-2.4.4.jar;C:\Users\userName.m2repository\org\codehaus\groovy\groovy-xml\2.4.4\groovy-xml-2.4.4.jar;C:\Users\userName.m2repository\org\apache\httpcomponents\httpmime\4.5.1\httpmime-4.5.1.jar;C:\Users\userName.m2repository\org\ccil\cowantagsouptagsoup\1.2.1tagsoup-1.2.1.jar;C:\Users\userName.m2repository\com\jayway\restassured\json-path\2.9.0\json-path-2.9.0.jar;C:\Users\userName.m2repository\org\codehaus\groovy\groovy-json\2.4.4\groovy-json-2.4.4.jar;C:\Users\userName.m2repository\com\jayway\restassured\rest-assured-common\2.9.0\rest-assured-common-2.9.0.jar;C:\Users\userName.m2repository\com\jayway\restassuredxml-path\2.9.0xml-path-2.9.0.jar;C:\Users\userName.m2repository\com\googleprotobufprotobuf-java\3.3.0protobuf-java-3.3.0.jar;C:\Users\userName.m2repository\comfasterxml\jackson\core\jackson-core\2.9.0\jackson-core-2.9.0.jar;C:\Users\userName.m2repository\comfasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;C:\Users\userName.m2repository\comfasterxml\jackson\core\jackson-databind\2.9.0\jackson-databind-2.9.0.jar;C:\Users\userName.m2repository\com\googlecode\json-simple\json-simple\1.1.1\json-simple-1.1.1.jar;C:\Users\userName.m2repository\org\apache\commons\commons-lang3\3.4\commons-lang3-3.4.jar;C:\Users\userName.m2repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;C:\Users\userName.m2repository\org\jsoup\jsoup\1.10.2\jsoup-1.10.2.jar;C:\Users\userName.m2repositoryxml-apisxml-apis\1.4.01xml-apis-1.4.01.jar;C:\Users\userName.m2repository\com\auth0\java-jwt\2.1.0\java-jwt-2.1.0.jar;C:\Users\userName.m2repository\org\json\json\20160810\json-20160810.jar;C:\Users\userName.m2repository\junit\junit\4.12\junit-4.12.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.3.4\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain steps.storyboardweb.TestForElementInvisibility
Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 38985
Only local connections are allowed.
Nov 20, 2017 11:05:09 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for invisibility of Proxy element for: DefaultElementLocator 'By.id: some id' (tried for 10 second(s) with 500 MILLISECONDS interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:82)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:231)
at steps.storyboardweb.TestForElementInvisibility.waitUntilElementGetsInvisible(TestForElementInvisibility.java:36)
at steps.storyboardweb.TestForElementInvisibility.main(TestForElementInvisibility.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"some id"}
(Session info: chrome=62.0.3202.94)
(Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.7.1', revision: '8a0099a', time: '2017-11-06T21:01:39.354Z'
System info: host: 'userName', ip: '192.168.0.102', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_111'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptSslCerts: true, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.33.506120 (e3e53437346286..., userDataDir: C:\Users\userNameAppData\Loca...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 62.0.3202.94, webStorageEnabled: true}
Session ID: 80f68c126d75deca24730ebee5c989f4
* Element info: {Using=id, value=some id}
at sun.reflect.GeneratedConstructorAccessor8.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:600)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:370)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:416)
at org.openqa.selenium.By$ById.findElement(By.java:218)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:362)
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy4.isDisplayed(Unknown Source)
at org.openqa.selenium.support.ui.ExpectedConditions.isInvisible(ExpectedConditions.java:1380)
at org.openqa.selenium.support.ui.ExpectedConditions.access$400(ExpectedConditions.java:43)
at org.openqa.selenium.support.ui.ExpectedConditions$46.apply(ExpectedConditions.java:1368)
at org.openqa.selenium.support.ui.ExpectedConditions$46.apply(ExpectedConditions.java:1364)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:208)
... 7 more
Process finished with exit code 1
_
Use following custom method to wait until element gets invisible :
protected void waitUntilElementGetsInvisible(WebElement elementTobeInvisible) {
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.invisibilityOf(elementTobeInvisible));
}
Recently I've upgraded from 2.53 version. It was working fine previously.
I've checked visibility of same element using element.isDisplayed() it returns 'false' correctly.
Also please check my further comments in this post, I think the problem is because of incorrect implementation of the method
Investigated the issue:
for version 2.53.0 and 3.7.0, there is implementation difference for invisibility method.
2.53 Implementation:
`public static ExpectedCondition
return new ExpectedCondition
public Boolean apply(WebDriver webDriver) {
Iterator var2 = elements.iterator();
while(var2.hasNext()) {
WebElement element = (WebElement)var2.next();
try {
if (element.isDisplayed()) {
return false;
}
} catch (Exception var5) {
;
}
}
return true;
}
public String toString() {
return "invisibility of all elements " + elements;
}
};
}`
Observation: Look at line "catch (Exception var5) ". Here we are catching everything as an Exception
for 3.7.1 implementation
`public static ExpectedCondition
return new ExpectedCondition() {
public Boolean apply(WebDriver webDriver) {
return Boolean.valueOf(elements.stream().allMatch((x$0) -> {
return ExpectedConditions.isInvisible(x$0);
}));
}
public String toString() {
return "invisibility of all elements " + elements;
}
};
}
private static boolean isInvisible(WebElement element) {
try {
return !element.isDisplayed();
} catch (**StaleElementReferenceException** var2) {
return true;
}
}`
Observation: We are only catching StaleElementReferenceException, hence while using the method throws TimeoutException Exception
For issues please provide a concise reproducible test case and describe what results you are seeing and what results you expect.
See CONTRIBUTING.md
Do you still need any more inputs?
We need a reproduction scenario, as mentioned in the above comment.
Can you please go through the post description again. I've added more details.
Instead of a narrative description (or in addition to it) please provide:
1) an executable scenario that can be copy-pasted to IDE, started and that fails because actual behaviour does not match expectations,
2) a sample page that is used in this scenario.
Is that suffice now?
@barancev : can you go through the issue again, please. Added some more information
@Ankit-Laddha,
I think the current behavior is correct.
Your example code waits for an element to become invisible (meaning the element exists in the DOM but is not displayed). Since the element you are waiting on ("some id") never exists at all, it just times out waiting for the expected condition to resolve.
@cgoldberg : Thanks. That explains it well. One quick question, was there any reason why we were catching 'Exception' till 2.53 version for invisible method?
It's quite possible there was a bug in 2.53. Suppressing all exceptions is dangerous. If the driver dies, or network error occurs, or the thread is interrupted -- we should not continue waiting.
Today had the same problem. If I understood correctly then point of the problem it
@FindBy(id = "some id")
public static WebElement element;
"element" - it's not WebElement it just proxy to the element with id ("some id"). So it can't be used for waiting.
Change:
ExpectedConditions.invisibilityOf(element)
to
ExpectedConditions.invisibilityOf($(By.id("some id")))
or
ExpectedConditions.invisibilityOf(driver.findElement(By.id("some id")))
it should help you.
If someone from SeleniumHD can explain to me why WebDriverWait working incorrectly with a proxy (it the same library package and should not be any problem)
Most helpful comment
@Ankit-Laddha,
I think the current behavior is correct.
Your example code waits for an element to become invisible (meaning the element exists in the DOM but is not displayed). Since the element you are waiting on ("some id") never exists at all, it just times out waiting for the expected condition to resolve.