Get HTML Source of WebElement in Selenium Webdriver using Python

Get HTML Source of WebElement in Selenium Webdriver using Python,in this lesson you will get complete selenium python code to print or get html source of web Element with the help of innerHtml attribute to get source of the content of the element or outerHtml for source with the current element.

ALSO READ  Selenium Grid Appium configuration Example

Selenium Webdriver Using Python – Complete Tutorial Guide
How to get the number of Elements in a list in Python

Get HTML Source of WebElement in Selenium Webdriver using Python

element.get_attribute('innerHTML')
import unittestfrom selenium 
import webdriverfrom selenium.webdriver.common.keys 
import Keysclass PythonOrgSearch

(unittest.TestCase):    
def setUp(self):        
self.driver = webdriver.Firefox()
def test_search_in_python_org(self):        
driver = self.driver        
driver.get("http://www.python.org")        
elem = driver.find_element_by_xpath("//*")

source_code = elem.get_attribute("outerHTML")    
def tearDown(self):        
self.driver.close()
if __name__ == "__main__":   

unittest.main()

If you want to save it to a file,use below code.

f = open('c:/htmlfile.html', 'w')
f.write(source_code.encode('utf-8'))
f.close()

Rajesh K

Leave a Reply

Your email address will not be published. Required fields are marked *