Selenium: Get HTML of a Web Page

5 Min Read

In this article, we will discuss how to use Selenium WebDriver to retrieve the HTML source code of a web page. Selenium is a powerful browser automation tool that allows you to interact with web pages, automate repetitive tasks, and perform web testing. By getting the HTML source code of a page, you can inspect the structure and content of the page, which is essential for web scraping, debugging, and testing purposes.

Accessing the HTML Source Code with Selenium WebDriver

Before we dive into the code, make sure you have installed Selenium and the appropriate WebDriver for the browser you plan to use. For this tutorial, we will use Python and the Chrome WebDriver, but the steps can be easily adapted for other programming languages and browsers. For more information on installing and configuring Selenium, check out our Python web scraping guide.

1. Import Selenium and Initialize WebDriver

First, you need to import the necessary modules and initialize the WebDriver. Here’s how to do that:


from selenium import webdriver

# Set the path to the ChromeDriver executable
chrome_driver_path = "/path/to/chromedriver"

# Create an instance of Chrome WebDriver
driver = webdriver.Chrome(executable_path=chrome_driver_path)

2. Navigate to the Web Page

Next, use the `get()` method to navigate to the desired web page:


url = "https://example.com"
driver.get(url)

3. Retrieve the HTML Source Code

Once the WebDriver has loaded the web page, you can access its HTML source code using the `page_source` attribute:


html_source = driver.page_source
print(html_source)

The `page_source` attribute returns the entire HTML source code of the page as a string. You can now process the HTML, extract specific elements or attributes, or save it to a file for further analysis.

4. Close the WebDriver

After you have finished working with the web page, it’s essential to close the WebDriver to free up resources and prevent any memory leaks:


driver.quit()

Working with the Retrieved HTML Source Code

Now that you have the HTML source code of the page, you can use a parsing library like BeautifulSoup to extract the information you need. Here’s an example of how to do this:

1. Install and Import BeautifulSoup

First, install the BeautifulSoup library using pip:


pip install beautifulsoup4

Then, import the necessary modules in your Python script:


from bs4 import BeautifulSoup

2. Parse the HTML Source Code

Create a BeautifulSoup object using the HTML source code you retrieved earlier and specify the parser you want to use (in this case, ‘html.parser’):


soup = BeautifulSoup(html_source, 'html.parser')

3. Extract Information from the HTML

With the BeautifulSoup object, you can

now access and extract various elements and attributes from the HTML source code. For example, to get all the links (anchor tags) on the page, you can use the `find_all()` method:


links = soup.find_all('a')

for link in links:
    print(link.get('href'))

This code snippet prints the ‘href’ attribute of all anchor tags found on the page.

4. Save the HTML Source Code to a File

If you want to save the retrieved HTML source code to a file for further analysis or archiving, you can do so using the following code:


file_name = "output.html"

with open(file_name, "w", encoding="utf-8") as file:
    file.write(html_source)

This code snippet writes the HTML source code to a file named ‘output.html’ in the same directory as your Python script. You can then open this file in your favorite text editor or web browser for further inspection.

Conclusion

In this article, we’ve learned how to use Selenium WebDriver to retrieve the HTML source code of a web page. This technique is essential for web scraping, debugging, and web testing purposes. Remember always to close the WebDriver after you have finished working with the web page to free up resources and prevent memory leaks. Happy coding!

For more web development tips and tutorials, be sure to check out our other articles:

Share this Article
Leave a comment