0

I´m writing a code that should take screenshots from 3 different breakpoints [1280 px, 768px and 320px]. But the code i have take 3 screenshots of the same screen widht.

from datetime import datetime
from selenium import webdriver
from pyvirtualdisplay import Display
import time
import argparse
from selenium.webdriver.chrome.options import Options

now = datetime.now().strftime('%d-%m-%Y_%H-%M-%S')

class ScreenshotMaker(object):
    def __init__(self, url):
        self.url = url
        self.display = Display()
        self.display.start()
        self.driver = webdriver.Chrome('/usr/local/bin/chromedriver')
        self.driver.set_page_load_timeout(30)
        self.driver.get(url)
        self.driver.maximize_window()

    def make_screenshots(self):
        # Make Screenshot for 1280
        self.display = Display(visible=0, size=(1280, 800)).start()
        self.driver.get_screenshot_as_file('Breakpoint_1280_Screenshot_%s.png' %(now))
        print("Taking Screenshot for Breakpoint 1280")

        # Make Screenshot for 768
        self.display = Display(visible=0, size=(768, 800)).start()
        self.driver.get_screenshot_as_file('Breakpoint_768_Screenshot_%s.png' %(now))
        print("Taking Screenshot for Breakpoint 768")

        # Make Screenshot for 320
        self.display = Display(visible=0, size=(320, 800)).start()
        self.driver.get_screenshot_as_file('Breakpoint_320_Screenshot_%s.png' %(now))
        print("Taking Screenshot for Breakpoint 320")

def main():
    # url = ''
    sm = ScreenshotMaker('https://example.com')
    sm.make_screenshots()

if __name__ == "__main__":
    main()

What do i have to change that the code takes 3 different screenshots?

Thanks for your help :)

diem_L
  • 389
  • 5
  • 22
  • You probably need to call `self.driver.maximize_window()` after setting the new display. – Florent B. Jul 27 '17 at 12:23
  • didn´t work .. :/ – diem_L Jul 27 '17 at 12:53
  • Rather than changing the display, you could just change the window size with `driver.set_window_size(w, h)`. – Florent B. Jul 27 '17 at 12:55
  • tried that already, but this change only the size of the window and doesn´t change the breakpoint .. – diem_L Jul 27 '17 at 13:45
  • The size of the window is limited to the size of the display. So first set the display to the maximum size (1280, 800) and then change the size of the window with `driver.set_window_size(w, h)`. – Florent B. Jul 27 '17 at 14:14
  • ok got it .. i had to write `self.driver.set_windwow_size(w, h)` before `self.display = Display(visible=0, size=(w, h)` .. thanks for your help – diem_L Jul 28 '17 at 05:23

0 Answers0