
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 to get innerHTML of whole page in selenium driver?
We can get the innerHTML of the whole page in Selenium. We shall use the method getPageSource and print the values captured by it in the console.
Syntax
String s = driver.getPageSource();
We can also get the HTML source code via Javascript commands in Selenium. We shall utilize executeScript method and pass the command return document.body.innerHTML as a parameter to the method.
Syntax
JavascriptExecutor j = (JavascriptExecutor) driver; String s = (String) j.executeScript("return document.body.innerHTML;");
Example
Code Implementation with getPageSource.
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 PageHTMLcode{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); String url = "https://www.tutorialspoint.com/index.htm"; driver.get(url); // getPageSource method to obtain HTML of page System.out.println("Get HTML of page: "+ driver.getPageSource()); driver.quit(); } }
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 PageHTMLcodeJS{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); String url = "https://www.tutorialspoint.com/index.htm"; driver.get(url); // Javascript executor to obtain page HTML JavascriptExecutor j = (JavascriptExecutor) driver; String s = (String) j.executeScript("return document.body.innerHTML;"); System.out.println("Get HTML of page: "+ s); driver.quit(); } }
- Related Questions & Answers
- How to get userAgent information in Selenium Web driver?
- How to get text with selenium web driver in python?
- How to get content of entire page using Selenium?
- Get text using selenium web driver in python?
- How to get innerHTML of a div tag using jQuery?
- 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 change user agent for Selenium driver?
- What is Selenium Internet Explorer Driver or IE Driver?
- How to get the innerHTML of a cell with JavaScript DOM?
- How to get the title and URL of the page in Selenium with python?
- What is selenium web driver?
- How do you get selenium to recognize that a page loaded?
- What is Web Driver in Selenium?
- How to get page source as it is in browser using selenium?
Advertisements