4

I have to write a Python code that will get URL, open a Chrome/Firefox browser using Selenium and will download it as a "Complete Webpage", mean with the CSS assets for example.

I know the basis of using Selenium, like:

from selenium import webdriver

ff = webdriver.firefox()
ff.get(URL)

ff.close

How can I perform the downloading action (Like clicking automatically in the browser CTRL+S)?

Dan
  • 689
  • 2
  • 10
  • 20
  • Found an answer: https://stackoverflow.com/questions/53729201/save-complete-web-page-incl-css-images-using-python-selenium – Gabriel Chung Apr 26 '20 at 09:04

1 Answers1

1

You can try following code to get HTML page as file:

from selenium import webdriver

ff = webdriver.Firefox()
ff.get(URL)
with open('/path/to/file.html', 'w') as f:
    f.write(ff.page_source)
ff.close
Andersson
  • 47,234
  • 13
  • 52
  • 101
  • this one saves just the source code whereas i need the whole page – Dan Dec 28 '16 at 15:29
  • Yes, this is exactly what you get if you use `CTRL+S`. And what do you mean "whole page"? – Andersson Dec 28 '16 at 15:33
  • when you clicking CTRL+S you get the whole page, mean that it will include the Page's CSS for example. While downloading the page source you will get just the source code as a text (The relative CSS won't be downloaded) – Dan Dec 28 '16 at 15:40
  • With `CTRL+S` you can download `HTML` (source code) only! `.css`, `.js` files are not part of "whole page", but separate files that browser should request from server while `.html` file parsing and page rendering – Andersson Dec 28 '16 at 16:24
  • Hi, Thanks for your answer but I'm 100% sure that when you click on `CTRL+S` you have an option named "complete web page" that will download the CSS files too. (I just did it manually right now and want to perform this by automated script) – Dan Dec 29 '16 at 08:00
  • Oh, yeap, sorry, now I see that option :) Initially, I thought you want to get the whole page as a single file... now it make sense. – Andersson Dec 29 '16 at 10:25