- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Accessing HTML source code using Python Selenium.
We can access HTML source code with Selenium webdriver. We can take the help of the page_source method and print the value obtained from it in the console.
Syntax
src = driver.page_source
We can also access the HTML source code with the help of Javascript commands in Selenium. We shall take the help of execute_script method and pass the command return document.body.innerHTML as a parameter to the method.
Syntax
h = driver.execute_script("return document.body.innerHTML;")
Example
Code Implementation.
from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/index.htm") # access HTML source code with page_source method s = driver.page_source print(s)
Code Implementation with Javascript Executor.
from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/index.htm") # access HTML source code with Javascript command h = driver.execute_script("return document.body.innerHTML") print(h)
- Related Articles
- Accessing Source code of SAP Transport without using SAP system
- Get HTML Source of WebElement in Selenium WebDriver using Python.
- How to retrieve source code from Python objects?
- Accessing an SAP endpoint in apex code
- How to get HTML code of a WebElement in Selenium?
- How to get HTTP Response Code using Selenium WebDriver?
- Source code of ABAP reports without restriction
- Rearrange the given source code in C++
- How to get page source as it is in browser using selenium?
- HTML DOM source object
- Accessing the internet using the urllib.request module in Python
- How to embed a video using HTML code?
- Displaying Source code of RFC in SAP system
- Using Extensions with Selenium & Python
- Read the Source Code of Shell Commands on Linux

Advertisements