- 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
How to get content of entire page using Selenium?
We can get the content of the entire page using Selenium. There are more than one ways of achieving it. To get the text of the visible on the page we can use the method findElement(By.tagname()) method to get hold of . Next can then use the getText() method to extract text from the body tag.
Syntax −
WebElement l=driver.findElement(By.tagName("body")); String t = l.getText();
The next approach to get the content of the entire page is to use the getPageSource() method.
Syntax −
String l = driver.getPageSource();
Example
Code Implementation with <body> tag.
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 TextContent{ 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.google.com/"; driver.get(url); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // identify element and input text inside it WebElement l =driver.findElement(By.tagName("body")); System.out.println("Text content: "+ l.getText()); driver.quit(); } }
Output
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 PageSrc{ 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.google.com/"; driver.get(url); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // getPageSource() and print String l = driver.getPageSource(); System.out.println("Page source: "+ l); driver.quit(); } }
- Related Articles
- How to capture the screenshot of a specific element rather than entire page using Selenium Webdriver?
- How to get the content of href within some targeted class using selenium?
- How to get the total number of checkboxes in a page using Selenium?
- How to get page source as it is in browser using selenium?
- Get page title with Selenium WebDriver using Java.
- How to get the total number of radio buttons on a page using Selenium?
- How to get innerHTML of whole page in selenium driver?
- How to obtain the page title using Selenium webdriver?
- How do you get selenium to recognize that a page loaded?
- How to get the complete screenshot of a page in Selenium with python?
- How to set Page Load Timeout using C# using Selenium WebDriver?
- How to get the content of a textarea using jQuery?
- How to get HTML content of an element using jQuery?
- How to get the value of div content using jQuery?
- How to automate instagram login page using java in selenium?

Advertisements