Python WebDriver how to print the whole page source (html)

I am using Python 2.7 with Selenium WebDriver. My question is how to print the entire page source using the print method. There is a webdriver method page_source, but it returns a WebDriver, and I don’t know how to convert it to String or just print it in the terminal

+7
python selenium-webdriver webdriver
source share
2 answers

.page_source in the .page_source instance is what you need:

 >>> from selenium import webdriver >>> driver = webdriver.Firefox() >>> driver.get('http://google.com') >>> print(driver.page_source) <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" itemtype="http://schema.org/WebPage" itemscope=""><head><meta name="descri ... :before,.vscl.vslru div.vspib{top:-4px}</style></body></html> 
+22
source share

You can also get the source of the HTML page without using a browser. The query module allows you to do this.

  import requests res = requests.get('https://google.com') res.raise_for_status() # this line trows an exception if an error on the # connection to the page occurs. print(res.text) 
0
source share

All Articles