- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Wait for complex page with JavaScript to load using Selenium.
We can wait for a complex page with JavaScript to load with Selenium. After the page is loaded, we can invoke the Javascript method document.readyState and wait till complete is returned.
Syntax
JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("return document.readyState").toString().equals("complete");
Next, we can verify if the page is ready for any action, by using the explicit wait concept in synchronization. We can wait for the expected condition presenceOfElementLocated for the element. We shall implement the entire verification within the try catch block.
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; import org.openqa.selenium.JavascriptExecutor; public class PageLoadWt{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.tutorialspoint.com/index.htm"); // Javascript Executor to check page ready state JavascriptExecutor j = (JavascriptExecutor)driver; if (j.executeScript ("return document.readyState").toString().equals("complete")){ System.out.println("Page loaded properly."); } //expected condition presenceOfElementLocated WebDriverWait wt = new WebDriverWait(driver,3); try { wt.until(ExpectedConditions .presenceOfElementLocated (By.id("gsc−i−id1"))); // identify element driver.findElement (By.id("gsc−i−id1")).sendKeys("Selenium"); } catch(Exception e) { System.out.println("Element not located"); } driver.quit(); } }
Output
- Related Articles
- How do you make Selenium 2.0 wait for the page to load?
- Wait until page is loaded with Selenium WebDriver for Python.
- How to wait for iFrames to load completely in Selenium webdriver?
- How to set Page Load Timeout using C# using Selenium WebDriver?
- How page load time affects with JavaScript?
- How to know the exact time to load a page using Selenium WebDriver?
- Need to wait until page is completely loaded - Selenium WebDriver
- Best practice to wait for a change with Selenium Webdriver?
- Wait for an Ajax call to complete with Selenium 2 WebDriver.
- How to get Selenium to wait for ajax response?
- Get page title with Selenium WebDriver using Java.
- How to load a page in div using jQuery?
- Selenium with C Sharp - How to perform Explicit Wait method?
- What is implicit wait in Selenium with python?
- Manipulate two selects on page load with jQuery

Advertisements