Question
What is the method getPageSource() in Selenium WebDriver, and how do I implement it using Java?
String pageSource = driver.getPageSource();
Answer
The getPageSource() method in Selenium WebDriver retrieves the complete HTML source of the current page that is loaded in the browser. This is particularly useful for evaluating the state of the web page after certain actions or for validation in automated tests.
String pageSource = driver.getPageSource(); // Retrieves HTML source of the current page
System.out.println(pageSource);
Causes
- Fetching dynamic page content that may not be immediately visible in the web inspector.
- Validating changes after form submissions or JS-driven content updates.
- Collecting complete DOM structure for further analysis.
Solutions
- 1. Use the getPageSource() method correctly: Assign the returned string to a variable as shown below.
- 2. Ensure your WebDriver is correctly initialized and a page is loaded before calling this method.
Common Mistakes
Mistake: Calling getPageSource() before the webpage is fully loaded.
Solution: Ensure you wait for the page to load completely using waits or a sleep method, especially for dynamic content.
Mistake: Not checking for element availability after navigating to a new page before fetching the source.
Solution: Use WebDriverWait to wait for specific elements to become available before calling getPageSource().
Helpers
- Selenium WebDriver
- getPageSource() method
- Java automation
- Selenium tutorial
- WebDriver page source retrieval