- 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
How do you get selenium to recognize that a page loaded?
We can get Selenium to recognize that a page is loaded. We can set the implicit wait for this purpose. It shall make the driver to wait for a specific amount of time for an element to be available after page loaded.
Syntax
driver.manage().timeouts().implicitlyWait();
After the page is loaded, we can also invoke Javascript method document.readyState and wait till complete is returned.
Syntax
JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("return document.readyState").toString().equals("complete");
After this, verify if the URL matches the one we are looking for.
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 Pageload{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "https://www.tutorialspoint.com/index.htm"; driver.get(url); // wait of 12 seconds driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // identify element, enter text WebElement m=driver.findElement(By.id("gsc-i-id1")); m.sendKeys("Selenium"); } }
Example
Code Implementation with Javascript Executor.
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; import org.openqa.selenium.JavascriptExecutor; public class PagaLoadJS{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "https://www.tutorialspoint.com/index.htm"; driver.get(url); // Javascript executor to return value JavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript("return document.readyState") .toString().equals("complete"); // get the current URL String s = driver.getCurrentUrl(); // checking condition if the URL is loaded if (s.equals(url)) { System.out.println("Page Loaded"); System.out.println("Current Url: " + s); } else { System.out.println("Page did not load"); } driver.quit(); } }
Output
- Related Articles
- Need to wait until page is completely loaded - Selenium WebDriver
- How do you make Selenium 2.0 wait for the page to load?
- How do I get IntelliJ to recognize common Python modules?
- Wait until page is loaded with Selenium WebDriver for Python.
- How to get content of entire page using Selenium?
- How do you test pages that require authentication with Selenium?
- How to get innerHTML of whole page in selenium driver?
- How do you get the footer to stay at the bottom of a Web page in HTML?
- How to get the complete screenshot of a page in Selenium with python?
- How to get the total number of checkboxes in a page using Selenium?
- How to get the total number of radio buttons on a page using Selenium?
- How to get page source as it is in browser using selenium?
- How do you use Selenium to execute javascript within a frame?
- Get page title with Selenium WebDriver using Java.
- How do you check scroll position using selenium?

Advertisements