22

I'm using Python 2.7 with Selenium WebDriver. My question is how to print whole page source with print method. There is webdriver method page_source but it returns WebDriver and I don't know how to convert it to String or just print it in terminal

wmarchewka
  • 383
  • 1
  • 3
  • 6

2 Answers2

43

.page_source on a webdriver 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>
alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148
  • 2
    Thank You, this is exactly what I need! That was my fault because I've done this in bad way `print driver.page_source` (driver.page_source wasn't in brackets) – wmarchewka Dec 10 '14 at 22:23
  • page_source only seems to be what is visible, not the entire html source. – Zach Jul 25 '18 at 04:52
-3

You can also get the HTML page source without using a browser. The requests module allows you to do that.

 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)
Myke
  • 7