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 webelement with the help of innerHtml attribute to get source of the content of the element or outerHtml for source with the current element.

Below is the code:


element.get_attribute('innerHTML')


import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class 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()

No comments:

Post a Comment