- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Equivalent of waitForVisible/waitForElementPresent in Selenium WebDriver tests using Java?
There are equivalent methods for waitForVisible/waitForElementPresent in Selenium webdriver. They are a part of the synchronization concept in Selenium.
The implicit and explicit waits are the two types of waits in synchronization.
The implicit wait is the wait applied to the webdriver for a specified amount of time for all elements. Once this time has elapsed and an element is still not available, an expectation is thrown.
Syntax
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Example
Code Implementation with implicit wait
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class ImplctWait{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //implicit wait of 5 secs driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //URL launch driver.get("https://www.tutorialspoint.com/index.htm"); // identify element WebElement m = driver.findElement(By.tagName("h4")); System.out.println("Element text is: " + m.getText()); driver.quit(); } }
Output
The explicit wait is the wait applied to the webdriver for a specified amount of time for an expected condition to be satisfied for an element. Once this time has elapsed and the expected criteria is not satisfied, an exception is thrown.
An equivalent to waitForVisible method can be the visibilityOfElementLocated expected condition in explicit wait. Also, an equivalent to waitForElementPresent method can be the presenceOfElementLocated expected condition.
Syntax
WebDriverWait w= (new WebDriverWait(driver,5 )); w.until(ExpectedConditions.presenceOfElementLocated(By.id("txt"))); w.until(ExpectedConditions.visibilityOfElementLocated (By.name("nam-txt")));
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class ExplicitWt{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //launch URL driver.get("https://www.tutorialspoint.com/index.htm"); //explicit wait condition - presenceOfElementLocated WebDriverWait w= (new WebDriverWait(driver, 7)); w.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[text()='Library']"))); WebElement m = driver.findElement(By.xpath("//*[text()='Library']")); m.click(); //explicit wait condition - visibilityOfElementLocated w.until(ExpectedConditions.visibilityOfElementLocated (By.linkText("Subscribe to Premium"))); WebElement n = driver.findElement(By.linkText("Subscribe to Premium")); String s = n.getText(); System.out.println("Text is: " + s); driver.quit(); } }
Output
- Related Articles
- Switch tabs using Selenium WebDriver with Java.
- How to handle frame in Selenium WebDriver using java?
- How to use clickandwait in Selenium Webdriver using Java?
- How to select checkboxes using selenium java webdriver?
- Get page title with Selenium WebDriver using Java.
- Capturing browser logs with Selenium WebDriver using Java.
- How to perform mouseover function in Selenium WebDriver using Java?
- How to type in textbox using Selenium WebDriver with Java?
- How to scroll down using Selenium WebDriver with Java?
- File Upload using Selenium WebDriver and Java Robot Class.
- Selenium WebDriver With Java Quickstart.
- Find elements using Selenium WebDriver?
- How to close child browser window in Selenium WebDriver using Java?
- How to simulate Print screen button using selenium webdriver in Java?
- How to automate gmail login process using selenium webdriver in java?
