0

This code save full web page, but I need to save just first view that user see(without scrolling down). Is it possible?

browser = webdriver.PhantomJS()
browser.set_window_size(1920, 1080)
browser.set_page_load_timeout(60)
browser.get(url)
browser.save_screenshot(png_path)

Update:

Here is example of save_screenshot of https://github.com/keras-team/keras/issues/3223, but I'm looking just for first view that user can see when he loads the page.

enter image description here

mrgloom
  • 20,061
  • 36
  • 171
  • 301

1 Answers1

0

To save the first view that user see, you have to tweak the configuration of pageLoadStrategy through DesiredCapabilities which is by default set as normal to none as follows :

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

caps = DesiredCapabilities().PHANTOMJS.copy()
caps["pageLoadStrategy"] = "none"
browser = webdriver.PhantomJS(desired_capabilities=caps, executable_path=r'C:\path\to\bin\phantomjs.exe')
browser.set_window_size(1920, 1080)
browser.set_page_load_timeout(60)
browser.get("https://accounts.google.com/signin")
browser.save_screenshot('./Screenshots/filename.png')

You can find a detailed discussion about pageLoadStrategy here.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Seems this don't work, for example I have tried `https://github.com/keras-team/keras/issues/3223` and it saved screenshot of full html page. – mrgloom Dec 20 '17 at 14:45
  • @mrgloom But your question was about the first view **not full view** I suppose. So my solution was `Selenium` based. – undetected Selenium Dec 20 '17 at 15:14
  • I'm not sure what you mean by 'first view', but in my terminology first view is upper part of full page. – mrgloom Dec 20 '17 at 15:18
  • @mrgloom So what was the issue with your own code which looks perfect? Can you share the URL as well? – undetected Selenium Dec 20 '17 at 15:24
  • The problem is that is saves full html page, but I need only upper part of it and I obviously can't load full page and then crop upper part for performace reasons. Test url: `https://github.com/keras-team/keras/issues/3223` – mrgloom Dec 20 '17 at 16:01
  • @mrgloom Can you narrow down to your exact requirement if you are looking for a snapshot of any particular WebElement. Probably I have a solution for you. – undetected Selenium Dec 20 '17 at 16:25
  • It should work for any web page, I don't know web element beforehand. – mrgloom Dec 20 '17 at 16:34