About this question
I am currently engaged in a particular task that is related to automating a project by using selenium and I need to extract and print the HTML content of a specific webpage. How can I achieve this by using selenium in Python?
I am currently engaged in a particular task that is related to automating a project by using selenium and I need to extract and print the HTML content of a specific webpage. How can I achieve this by using selenium in Python?
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask QA Expert
Answered on May 8, 2024
In the context of selenium, here is the example given of how you can achieve this by using the selenium in Python programming language:-
From selenium import webdriver
From selenium.webdriver.chrome.service import Service
From selenium.webdriver.common.by import By
From selenium.webdriver.support.ui import WebDriverWait
From selenium.webdriver.support import expected_conditions as EC# Set up the Chrome WebDriver (assuming you have downloaded the Chrome WebDriver executable)
Chrome_driver_path = “path_to_chromedriver.exe” # Update with your Chrome WebDriver path
Service = Service(chrome_driver_path) Driver = webdriver.Chrome(service=service)Try: # Navigate to the desired webpage
Driver.get(https://www.example.com) # Wait for the page to fully load
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, “body”))) # Get the HTML content of the page
Html_content = driver.page_source # Print the HTML content
Print(html_content)
Finally:
# Close the WebDriver
Driver.quit()Here is the example given in java programming language:-
Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.chrome.ChromeDriver;Public class PrintHTMLContent {
Public static void main(String[] args) {
// Set the path to the Chrome WebDriver executable
System.setProperty(“webdriver.chrome.driver”, “path_to_chromedriver.exe”); // Update with your Chrome WebDriver path
// Initialize the Chrome WebDriver
WebDriver driver = new ChromeDriver();
Try {
// Navigate to the desired webpage
Driver.get(https://www.example.com);
// Get the HTML content of the page
String htmlContent = driver.getPageSource();
// Print the HTML content
System.out.println(htmlContent);
} finally {
// Close the WebDriver
Driver.quit();
}
}
}Here is the example given by using HTML:-
<meta</span> charset=”UTF-8”>
<meta</span> name=”viewport” content=”width=device-width, initial-scale=1.0”>
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Step-by-step QA Testing guides from industry experts
Common QA Testing interview questions, answered
Guides, tips and career advice on QA Testing from JanBask experts.
Selenium Selenium Tutorial: Complete Guide to Automation Testing for Beginners
The Selenium tutorial covers both fundamental & advanced Selenium concepts. It is for individuals at all levels. Learn about…
QA Testing Software Testing Basics: Fundamentals, Principles, Lifecycle & Best Practices
Learn software testing basics, including key principles, lifecycle stages, types of testing, and best practices. A complete…
QA Testing You Can’t Miss the Top 20 Security Testing Interview Questions
Are you getting ready for your information security interview? Get a hold of the top 20 information security interview questions…