Get HTML source of web-element using Selenium web-driver in Python
Hello programmers, in this tutorial we will see how to get the HTML source of the web element using the Selenium web driver in Python.
Selenium is an open-source tool that is used in controlling web browsers via a program. The automation task can be done using various programming languages such as Python, Ruby, PHP, JavaScript, etc.
Here we will see a demo of how to get the HTML source of a web element using Selenium web driver in Python.
Installation
Download the selenium library using the following command in the command prompt.
pip install selenium
Basic Usage
HTML source – it is the code for a particular element in a web page.
Web element – elements that appear on a web page i.e. header tags, text-boxes, buttons, etc.
innerHTML – it is an attribute that returns the string which is present within the HTML tags.
outerHTML – it is an attribute that returns the string including the HTML tags present with it.
We have to install the chrome driver along with the executable file path location to the system path.
Given below is the illustration of using the ‘innerHTML’ attribute.
#Importing selenium library
from selenium import webdriver
from selenium.webdriver.common.by import By
#finding web element function
def seleniumFindWebElement():
#creating a webdriver object
driver = webdriver.Chrome(executable_path='C:/path/to/dir/chromedriver.exe')
driver.maximize_window() #maximize window size
driver.get("https://www.codespeedy.com/") #opening the url
ele = driver.find_element(by=By.CSS_SELECTOR, value='h2') #finding the element with 'h2'as its CSS
print(f"Web element with 'h2' as its CSS: {ele.get_attribute('innerHTML')}")
#driver
if __name__ == "__main__":
seleniumFindWebElement() #call the function
Output
Web element with 'h2' as its CSS: Some of Our Programming Blog Categories
Explanation
Inside the seleniumFindWebElement() function, the ‘.get()’ method is used for retrieving the URL. By using the ‘find_element()’, we find the first occurrence of the ‘<h2>’ tag element and print it using the ‘innerHTML’ attribute.
Given below is the illustration of using the ‘outerHTML’ attribute.
#Importing selenium library
from selenium import webdriver
from selenium.webdriver.common.by import By
#finding web element function
def seleniumFindWebElement():
#creating a webdriver object
driver = webdriver.Chrome(executable_path='C:/path/to/dir/chromedriver.exe')
driver.maximize_window() #maximize window size
driver.get("https://www.codespeedy.com/") #opening the url
ele = driver.find_element(by=By.CSS_SELECTOR, value='h2') #finding the element with 'h2'as its CSS
print(f"Web element with 'h2' as its CSS: {ele.get_attribute('outerHTML')}")
#driver
if __name__ == "__main__":
seleniumFindWebElement() #call the function
Output
Web element with 'h2' as its CSS: <h2>Some of Our Programming Blog Categories</h2>
Explanation
We get a similar output as above but as we are using the ‘outerHTML’ attribute, we get our output with the HTML tags.
Leave a Reply